use of geometry.Circle in project facetmodeller by pglelievre.
the class View3DPanel method paintComponent.
// -------------------- Overridden Methods --------------------
/**
* Paints graphics on the panel.
* @param g Graphics context in which to draw.
*/
@Override
public void paintComponent(Graphics g) {
// Only draw 3D if panel is open and ndim==3:
if (!controller.is3D() || !controller.getShowView3DPanel()) {
return;
}
// Remove image from the panel:
// removeAll();
// updateUI();
// Make sure the drawn flag is false:
drawn = false;
// Paint background:
super.paintComponent(g);
// Clear the list of projected nodes:
projectedPoints = null;
// Calculate the properties required for projection:
SceneInfo info = controller.getSceneInfo3D();
// no voi or no nodes in plc
if (info == null) {
return;
}
if (info.getScaling() == 0.0) {
return;
}
MyPoint3D spaceOrigin = info.getOrigin();
projector.setSpaceOrigin(spaceOrigin);
if (spaceOrigin == null) {
return;
}
// panel width
int w = getWidth();
// panel height
int h = getHeight();
double x = w;
double y = h;
// smallest panel dimension
double imageSizeScaling = Math.min(x, y);
// half panel width
x /= 2.0;
// half panel height
y /= 2.0;
// radius of circle surrounding the panel
double r = Math.sqrt(x * x + y * y);
MyPoint3D imageOrigin = new MyPoint3D(x, y, 0.0);
// imageOrigin.plus(controller.getPanX3D(),controller.getPanY3D(),0); // this does the panning via pan buttons
// this does the panning via dragging
imageOrigin.plus(getPanX(), getPanY(), 0);
projector.setImageOrigin(imageOrigin);
double sceneAndZoomScaling = info.getScaling() * zoomer.getScaling();
if (sceneAndZoomScaling == 0.0) {
return;
}
if (imageSizeScaling == 0.0) {
return;
}
projector.setSceneAndZoomScaling(sceneAndZoomScaling);
projector.setImageSizeScaling(imageSizeScaling);
dragSphere = new Circle(0, 0, r);
// Project all the nodes and reset the node IDs:
ModelManager model = controller.getModelManager();
int n = model.numberOfNodes();
projectedPoints = new MyPoint3D[n];
for (int i = 0; i < n; i++) {
// loop over each node
Node node = model.getNode(i);
// Set the node ID equal to i so I can use the ID's as indices into the projectedPoints array:
node.setID(i);
// Transform the point to image coordinates:
MyPoint3D p3 = node.getPoint3D();
MyPoint3D p;
if (p3 == null) {
// section not calibrated
p = null;
} else {
// p is a deep copy of node.getPoint3D()
p = spaceToImage(p3);
}
// Save the transformed point in the list:
projectedPoints[i] = p;
}
// If rotating then I'll only draw minimal information using 2D graphics,
// otherwise I'll draw everything using a zbuffer. I'll deal with this below
// by checking the status of the pt1 variable. I'll need some common information first:
// boolean showOther = controller.getShowOther(); // if true then draws outlines around other sections
// if true then draws outlines around other sections
boolean showOutlines = controller.getShowSectionOutlines();
// if false then only show nodes/facets associated with selected section(s)
boolean showAll = controller.getShowAllSections();
// Get drawing styles for overlays:
final int edgeWidth = controller.getLineWidth();
final int nodeWidth = controller.getPointWidth();
// final int centroidWidth = (int)Math.ceil(nodeWidth/2.0); // centroids drawn half the node width
final double normalLength = controller.getNormalLength();
final Color normalCol = controller.getNormalColor();
boolean normalThick = controller.getNormalThick();
boolean edgeThick = controller.getEdgeThick();
final int normalWidth;
if (normalThick) {
normalWidth = 8;
} else {
normalWidth = 4;
}
// Initialize the zbuffer or Graphics2D object:
Graphics2D g2 = (Graphics2D) g;
MyPoint3D pt1 = getPt1();
if (pt1 == null) {
zbuf = new ZBuffer3D(w, h, -Double.MAX_VALUE, getBackground());
} else {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setStroke(new BasicStroke(edgeWidth)); // line style for facet edges
w = nodeWidth;
}
// Get the other section(s) being displayed (if any exist):
Section currentSection = controller.getSelectedCurrentSection();
SectionVector otherSections;
// if ( showOther || showAll ) {
if (showOutlines || showAll) {
otherSections = controller.getSelectedOtherSections();
} else {
otherSections = null;
}
// Get the drawing options for colouring the nodes and facets:
int nodeColorBy = controller.getNodeColorBy();
int facetColorBy = controller.getFacetColorBy();
// Add facets to the scene: (this should be done first so that
// z-buffering can't draw a facet edge over a node)
boolean showFaces = controller.getShowFaces();
boolean showNormals = controller.getShowNormals();
boolean showNormalTails = controller.getShowNormalTails();
boolean showNormalHeads = controller.getShowNormalHeads();
if (pt1 == null) {
GroupVector facetGroups = controller.getSelectedFacetGroups();
if (facetGroups != null) {
for (int i = 0; i < facetGroups.size(); i++) {
// loop over each group of facets to display
Group group = facetGroups.get(i);
FacetVector facets = group.getFacets();
for (int j = 0; j < facets.size(); j++) {
// loop over each facet in the group
Facet facet = facets.get(j);
// Make sure that all the nodes in the facet are calibrated:
NodeVector nodes = facet.getNodes();
int nn = nodes.size();
// only 3D facets are drawn
if (nn < 3) {
continue;
}
boolean ok = true;
for (int k = 0; k < nn; k++) {
// loop over each node in the facet
Node node = nodes.get(k);
MyPoint3D p = node.getPoint3D();
// if the node's section is not calibrated
if (p == null) {
ok = false;
break;
}
if (!showAll) {
// Make sure the facet belongs to no other sections than those selected:
Section s = node.getSection();
// shouldn't be possible
if (s == null) {
ok = false;
break;
}
if (!s.equals(currentSection) && otherSections != null && !otherSections.contains(s)) {
ok = false;
break;
}
}
}
if (!ok) {
continue;
}
// Place the required transformed points into a new object:
n = facet.size();
MyPoint3D[] pts = new MyPoint3D[n];
for (int k = 0; k < n; k++) {
// loop over each node in the facet
// index into projectedPoints
int id = facet.getNode(k).getID();
pts[k] = projectedPoints[id];
if (pts[k] == null) {
// node is behind camera, or its section is not calibrated, so don't paint the facet
ok = false;
break;
}
}
if (!ok) {
continue;
}
// Calculate the shading:
MyPoint3D v = facet.getNormal();
if (v == null) {
continue;
}
// normalized
v = v.deepCopy();
if (v == null) {
continue;
}
// rotated into projected coordinates
v.rotate(projector.getRotationMatrix());
// dotted with the z axis (direction not important)
double d = Math.abs(v.getZ());
// too dark otherwise
d = 1.0 - (1.0 - d) * 0.8;
// Determine the painting colour for the facet:
Color col = getFacetPaintingColor(facetColorBy, facet);
// Apply the shading to the painting colour:
float[] hsb = new float[3];
Color.RGBtoHSB(col.getRed(), col.getGreen(), col.getBlue(), hsb);
// d is on [0,1]
hsb[2] *= (float) d;
col = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
// Process the facet through the zbuffer:
Color faceCol = null;
Color edgeCol;
if (showFaces) {
// Paint facet face as a coloured patch and paint edges as user requests:
faceCol = col;
edgeCol = controller.getEdgeColor();
} else {
// Paint edges of facet only, using facet colour:
// faceColor = null;
edgeCol = col;
}
zbuf.putFacet(pts, faceCol, edgeCol, edgeThick);
// Draw the facet normals:
if (showNormals) {
// Set up the points on either end of the normal vector line:
// facet centroid (point at start of normal vector line)
MyPoint3D p0 = facet.getCentroid().deepCopy();
if (p0 == null) {
continue;
}
// normalized normal vector (length of one)
MyPoint3D p1 = facet.getNormal().deepCopy();
if (p1 == null) {
continue;
}
// normal vector of spatial length normalLength
p1.times(normalLength);
// point at end of normal vector line
p1.plus(p0);
// Transform the points to image coordinates:
p0 = spaceToImage(p0);
p1 = spaceToImage(p1);
if (p0 == null) {
continue;
}
if (p1 == null) {
continue;
}
// Draw normal vectors as line objects:
if (pt1 == null) {
zbuf.putEdge(p0, p1, normalCol, normalThick);
// point at normal tail
if (showNormalTails) {
zbuf.putNode(p0, normalCol, normalWidth);
}
// point at normal head
if (showNormalHeads) {
zbuf.putNode(p1, normalCol, normalWidth);
}
} else {
g2.setPaint(normalCol);
g2.drawLine((int) p0.getX(), (int) p0.getY(), (int) p1.getX(), (int) p1.getY());
// circle at normal tail
if (showNormalTails) {
g2.fillOval((int) p0.getX() - normalWidth / 2, (int) p0.getY() - normalWidth / 2, normalWidth, normalWidth);
}
// circle at normal head
if (showNormalHeads) {
g2.fillOval((int) p1.getX() - normalWidth / 2, (int) p1.getY() - normalWidth / 2, normalWidth, normalWidth);
}
}
}
}
// for j
}
// for i
}
// if (facetGroups!=null)
}
// if (pt1==null) // not rotating
// Add nodes to the scene:
Composite defaultComposite = g2.getComposite();
if (pt1 != null) {
float alpha = (float) 0.5;
int type = AlphaComposite.SRC_OVER;
AlphaComposite composite = AlphaComposite.getInstance(type, alpha);
g2.setComposite(composite);
}
GroupVector nodeGroups = controller.getSelectedNodeGroups();
if (nodeGroups != null) {
for (int i = 0; i < nodeGroups.size(); i++) {
// loop over each group of nodes to display
Group group = nodeGroups.get(i);
NodeVector nodes = group.getNodes();
for (int j = 0; j < nodes.size(); j++) {
// loop over each node in the group
Node node = nodes.get(j);
Section s = node.getSection();
// shouldn't be possible
if (s == null) {
continue;
}
// not possible but I check for it anyway
if (!s.isCalibrated()) {
continue;
}
if (!showAll && !s.equals(currentSection) && otherSections != null && !otherSections.contains(s)) {
continue;
}
// index into projectedPoints
int id = node.getID();
// Determine the painting colour for the node:
Color col = getNodePaintingColor(nodeColorBy, node);
// Paint the node:
if (pt1 == null) {
if (projectedPoints[id] != null) {
// only paint if node is in front of the camera, and if its section is calibrated
zbuf.putNode(projectedPoints[id], col, controller.getPointWidth());
}
} else {
MyPoint3D p = spaceToImage(node.getPoint3D());
// to next iteration of for j
if (p == null) {
continue;
}
g2.setPaint(col);
g2.fillOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// for j
}
// for i
}
// Add regions to the scene:
if (pt1 == null) {
if (controller.getShowRegions()) {
for (int i = 0; i < model.numberOfRegions(); i++) {
Region region = model.getRegion(i);
Section s = region.getSection();
// shouldn't be possible
if (s == null) {
continue;
}
// not possible but I check for it anyway
if (!s.isCalibrated()) {
continue;
}
if (!showAll && !s.equals(currentSection) && otherSections != null && !otherSections.contains(s)) {
continue;
}
// Project the region point:
MyPoint3D p = spaceToImage(region.getPoint3D());
// region is behind camera
if (p == null) {
continue;
}
// Process the region through the zbuffer:
zbuf.putNode(p, region.getColor(), controller.getPointWidth());
}
}
}
// Draw the outlines of the selected sections:
if (pt1 == null) {
SectionVector sections = controller.getSelectedOtherSections();
// if ( showOther && sections!=null ) {
if (showOutlines && sections != null) {
for (int i = 0; i < sections.size(); i++) {
// loop over each section to display
Section section = sections.get(i);
// Construct the section outline in section image pixel coordinates:
// 4 corners
n = 4;
MyPoint2D[] pts2D = new MyPoint2D[n];
w = section.getWidth();
h = section.getHeight();
if (w <= 0 || h <= 0) {
continue;
}
w--;
h--;
pts2D[0] = new MyPoint2D(0, 0);
pts2D[1] = new MyPoint2D(w, 0);
pts2D[2] = new MyPoint2D(w, h);
pts2D[3] = new MyPoint2D(0, h);
// Transform the outline from 2D image pixels to space coordinates:
MyPoint3D[] pts3D = new MyPoint3D[n];
boolean ok = true;
for (int j = 0; j < n; j++) {
MyPoint3D p = section.imageCornerToSpace(pts2D[j]);
// null if section not calibrated
if (p == null) {
ok = false;
break;
}
pts3D[j] = p;
}
if (!ok) {
continue;
}
// Project the outline (from space to plotting pixel coordinates):
for (int j = 0; j < n; j++) {
MyPoint3D p = spaceToImage(pts3D[j]);
// point is behind camera
if (p == null) {
ok = false;
break;
}
pts3D[j] = p;
}
// for j
if (!ok) {
continue;
}
// Draw as black line objects:
for (int j = 0; j < n; j++) {
int j2 = j + 1;
if (j2 == n) {
j2 = 0;
}
zbuf.putEdge(pts3D[j], pts3D[j2], Color.BLACK);
}
}
// for i
}
}
// Draw the VOI:
if (pt1 != null) {
g2.setPaint(Color.BLACK);
g2.setComposite(defaultComposite);
}
if (controller.getShowVOI()) {
if (controller.hasVOI()) {
MyPoint3D[][] edges = controller.getVOIEdges();
for (int i = 0; i < VOI.N_EDGES; i++) {
MyPoint3D p1 = spaceToImage(edges[i][0]);
MyPoint3D p2 = spaceToImage(edges[i][1]);
// point(s) behind camera
if (p1 == null || p2 == null) {
continue;
}
if (pt1 == null) {
zbuf.putEdge(p1, p2, Color.BLACK);
} else {
g2.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(), (int) p2.getY());
}
}
// for i
}
}
// Draw the axes:
if (showAxes) {
// Set up axis points:
// origin
MyPoint3D po = spaceOrigin.deepCopy();
// x axis
MyPoint3D px = MyPoint3D.plus(po, new MyPoint3D(AXIS_LENGTH, 0, 0));
// y axis
MyPoint3D py = MyPoint3D.plus(po, new MyPoint3D(0, AXIS_LENGTH, 0));
// z axis
MyPoint3D pz = MyPoint3D.plus(po, new MyPoint3D(0, 0, AXIS_LENGTH));
// Project with no scaling:
po = spaceToImage(po, 1.0);
px = spaceToImage(px, 1.0);
py = spaceToImage(py, 1.0);
pz = spaceToImage(pz, 1.0);
// Move to bottom left corner if desired:
if (!centreAxes) {
po.minus(imageOrigin);
px.minus(imageOrigin);
py.minus(imageOrigin);
pz.minus(imageOrigin);
MyPoint3D p = new MyPoint3D(AXIS_LENGTH + 5, getHeight() - AXIS_LENGTH - 5, 0.0);
po.plus(p);
px.plus(p);
py.plus(p);
pz.plus(p);
}
// Draw axes as coloured line objects:
if (pt1 == null) {
zbuf.putEdge(po, px, Color.RED);
zbuf.putEdge(po, py, Color.YELLOW);
zbuf.putEdge(po, pz, Color.GREEN);
} else {
g2.setPaint(Color.RED);
g2.drawLine((int) po.getX(), (int) po.getY(), (int) px.getX(), (int) px.getY());
g2.setPaint(Color.YELLOW);
g2.drawLine((int) po.getX(), (int) po.getY(), (int) py.getX(), (int) py.getY());
g2.setPaint(Color.GREEN);
g2.drawLine((int) po.getX(), (int) po.getY(), (int) pz.getX(), (int) pz.getY());
}
}
// There is nothing more to draw if rotating:
if (pt1 != null) {
drawn = true;
return;
}
// Get the zbuffer image:
BufferedImage image = zbuf.getImage();
// Return if no image exists:
if (image == null) {
return;
}
// Tightly fit the image inside the panel (a very simple fitting since they are the same size!):
tightFitImage(g, image);
// Return if no sections exist:
if (!model.hasSections()) {
drawn = true;
return;
}
// Return if no current section exists:
if (currentSection == null) {
return;
}
// Figure out where the cursor is:
boolean mouseInside2D = controller.getMouseInside2D();
// Get mouse click mode:
int mode = controller.getClickMode();
// Redefine w for use below:
w = 2 * nodeWidth;
// Set stroke for facet edge overlays:
BasicStroke dashedStroke = new BasicStroke(edgeWidth + 1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, (float) 10.0, new float[] { 2f, 16f }, (float) 0.0);
g2.setStroke(dashedStroke);
g2.setPaint(controller.getDefineFacetEdgeColor());
// Paint the edges of the current facet being defined:
Facet currentFacet = controller.getCurrentFacet();
if (currentFacet != null) {
// Create a path around the node coordinates:
if (currentFacet.size() > 1) {
GeneralPath path = addFacetToPath(currentFacet);
if (path != null) {
// May want to close the path:
if (mode == ClickModeManager.MODE_DEFINE_TRI_FACETS || mode == ClickModeManager.MODE_ADD_NODES_IN_FACETS) {
path.closePath();
}
// Draw the path:
g2.draw(path);
}
}
}
// Paint the edges of the facet closest to the cursor position:
if (mouseInside2D && currentFacet == null) {
Facet facet = controller.getClosestFacet();
if (facet != null) {
// Create a path around the node coordinates:
if (facet.size() > 1) {
GeneralPath path = addFacetToPath(facet);
if (path != null) {
// Close the path:
path.closePath();
// Draw the path:
g2.draw(path);
}
}
}
}
// Set stroke for node overlays:
g2.setStroke(new BasicStroke(edgeWidth));
// Paint circles around the nodes of the current facet being defined:
if (currentFacet != null) {
if (mode == ClickModeManager.MODE_DEFINE_TRI_FACETS) {
g2.setPaint(Color.WHITE);
} else {
g2.setPaint(Color.BLACK);
}
for (int i = 0; i < currentFacet.size(); i++) {
Node node = currentFacet.getNode(i);
// Only paint the node if it is in the current or other sections:
Section section = node.getSection();
if (section.equals(currentSection) || (otherSections != null && otherSections.contains(section))) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p == null) {
continue;
}
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// for i
}
// Paint circles around the nodes of the facet closest to the cursor position:
if (mouseInside2D && currentFacet == null) {
Facet facet = controller.getClosestFacet();
if (facet != null) {
// Paint circles around the node coordinates:
g2.setPaint(Color.WHITE);
for (int i = 0; i < facet.size(); i++) {
Node node = facet.getNode(i);
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p == null) {
continue;
}
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
// for i
}
}
// Paint a white circle around the current node:
Node node = controller.getCurrentNode();
if (node != null) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p != null) {
g2.setPaint(Color.WHITE);
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// Paint a white circle around the node closest to the cursor position:
node = controller.getClosestNode();
if (mouseInside2D && node != null) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p != null) {
g2.setPaint(Color.WHITE);
g2.drawOval((int) p.getX() - w / 2, (int) p.getY() - w / 2, w, w);
}
}
// Redefine w for use below:
w = nodeWidth;
// Paint a cursor location indicator at the position of the candidate node:
node = controller.getCandidateNode();
if (node != null) {
MyPoint3D p = spaceToImage(node.getPoint3D());
if (p != null) {
g2.setPaint(Color.WHITE);
int ix = (int) p.getX();
int iy = (int) p.getY();
g2.drawOval(ix - w / 2, iy - w / 2, w, w);
g2.drawLine(ix - w / 2, iy, ix + w / 2, iy);
g2.drawLine(ix, iy - w / 2, ix, iy + w / 2);
}
}
// Set drawn flag to true:
drawn = true;
}
use of geometry.Circle in project facetmodeller by pglelievre.
the class ZBuffer3D method putNode.
// -------------------- Public methods -------------------
/**
* Processes a node.
* @param p3
* @param col
* @param d
*/
public void putNode(MyPoint3D p3, Color col, double d) {
// Create a circle object:
double r = d / 2.0;
Circle circle = new Circle(p3.getX(), p3.getY(), r);
// Get the bounding box:
BBox box = getBBox(p3, r);
// Loop over each pixel in the bounding box:
for (int i = box.i1; i < box.i2; i++) {
for (int j = box.j1; j < box.j2; j++) {
MyPoint2D p = new MyPoint2D(i, j);
// Check if the pixel is inside the projected sphere:
if (!circle.inside(p)) {
continue;
}
// Calculate the height of the pixel projected onto the sphere's surface:
// addition means closer to viewer
double z = p3.getZ() + circle.interpolate(p);
// Compare against the z value in the ZBuffer:
// the checking is done inside this method
setPixel(i, j, z, col);
}
}
}
Aggregations