copying MPoint

User 870ab5b546

19-08-2013 14:03:16

The code:


    public static boolean pointOnLine(DPoint3 p, MPolyline line,
boolean debugPrint) {
final String SELF = "VectorMath.pointOnLine: ";
// need to copy MPoint to get location to prevent infinite recursion in
// certain rare instances
final DPoint3 pt1 = (new MPoint(line.getPoint(0))).getLocation();
final DPoint3 pt2 = (new MPoint(line.getPoint(1))).getLocation();
final DPoint3 pt = new DPoint3(p.x, p.y, 0); // project onto XY plane
final DPoint3 vecTo1 = diff(new DPoint3(pt1.x, pt1.y, 0), pt);
final DPoint3 vecTo2 = diff(new DPoint3(pt2.x, pt2.y, 0), pt);
final double angle = angle(vecTo1, vecTo2);
final int endPt_pt_endPtAngle = MathMethods.roundToInt(
toDegrees(Math.abs(angle)));
if (debugPrint) debugPrint(SELF + "pt = ", p,
", original line endPt1 = ", line.getPoint(0).getLocation(),
", original line endPt2 = ", line.getPoint(1).getLocation(),
", endPt1 = ", pt1,
", endPt2 = ", pt2,
", vector to line origin = ", vecTo1,
", vector to line destination = ", vecTo2,
", raw endPt_pt_endPtAngle in radians = ", angle,
", rounded endPt_pt_endPtAngle in degrees = ",
endPt_pt_endPtAngle,
"; should be 180 +/- 10 to be found on line.");
return MathMethods.inRange(endPt_pt_endPtAngle, new int[] {170, 190});
} // pointOnLine(DPoint3, MPolyline, boolean)

Log output:


VectorMath.pointOnLine: pt = DPoint3(1.8591372966766357, 1.5148526430130005, 0.0), 
original line endPt1 = DPoint3(-3.55684831738472, 9.937028884887695, 0.0),
original line endPt2 = DPoint3(-3.4905203878879547, 8.560417175292969, 0.0),
endPt1 = DPoint3(0.0, 0.0, 0.0),
endPt2 = DPoint3(0.0, 0.0, 0.0),
vector to line origin = DPoint3(-1.8591372966766357, -1.5148526430130005, 0.0),
vector to line destination = DPoint3(-1.8591372966766357, -1.5148526430130005, 0.0),
raw endPt_pt_endPtAngle in radians = 0.0, rounded endPt_pt_endPtAngle in degrees = 0;
should be 180 +/- 10 to be found on line.

Seems to me that the when the MPoint is copied, its location should be copied as well, but it seems clear that the location is not being copied into the new object.  Am I missing something?

ChemAxon 2c555f5717

22-08-2013 13:53:54

Dear Bob!


 


   It is quite hard to answer your question since MPolyLine has a descendant, that behaves differently, and MPoint has many descendants (which behave differently) as well. I can tell some scenarios when your method will generate this behaviour for sure and others when it will work well.


   The problem is a misconcept by our code (which unfortuantely became our legacy) : there should not be copy constructor in any java code since it won't be polymorphic. It is possible that the MPolyLine returns an MAtomSetPoint which can not be copied properely by MPoint. But you can always call clone() which will clone the object properely.


   In the other hand MPolyLine has getPoint(int) and getPointRef(int) methods. The difference is that getPoint(int) clones the point object to ensure that the MObject won't be changed from outside. (As you can guess getPointRef(int) returns the point object itself.) This also means that you do not have to copy the returned point since it is a copy already.


   I hope it helped you!


 


Regards:
Balázs 

User 870ab5b546

23-08-2013 15:37:56

Yes, that's helpful.  I have in my notes that I have to copy the MPoint returned by line.getPoint(0) to avoid infinite recursion in some rare instances, so I'll just use clone() to copy it, even though it really oughtn't be necessary from what you say above.