Grasshopper

algorithmic modeling for Rhino

Hi everybody,


I am having trouble with null objects in a script which are starting to drive me ... 


I am trying to iterate intersections plane-curve but when it goes null I get the error "Object not set as an instance of an object", the script component turns red and I can´t skip it


The intersection function goes like this:


public Point3d Interseccion (Point3d origenInt, Curve curvaInt, Vector3d vectorXInt, Vector3d vectorYInt)
{
Point3d intersection = new Point3d();
   
Plane planoIntersec = new Plane(origenInt, vectorXInt, vectorYInt);

Rhino.Geometry.Intersect.CurveIntersections ci = Rhino.Geometry.Intersect.Intersection.CurvePlane(curvaInt, planoIntersec, 0.0);

if(ci[0] != null) //THIS IS THE PART THAT FAILS :-s
{
Point3d pTemp = new Point3d(ci[0].PointA.X, ci[0].PointA.Y, ci[0].PointA.Z);
intersection = pTemp;
}
return intersection;
}


Any clues of how to deal with this?


Cheers

Views: 543

Replies to This Discussion

Hi Miguel,

could you post the definition, please?
In general, I think that it could be caused by Curve being null, the non-existance of some intersection, etc...

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
sure, there it goes!

thanks, giulio
Attachments:
This might be a way to continue.

Also, remember that with Point3d's, after Point3d p = anotherP; p and anotherP are two different entities, one the copy of the other.
This is because Point3d is a structure, a small .Net ValueType.

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
Attachments:
Hi Miguel,

ci may well be null, so you can't even call ci[0].

You should add an extra test in there:

if (ci == null) { return; }

--
David Rutten
david@mcneel.com
Seattle, WA
Thanks, both of you. You´ve been really helpful!

Just to add a comment on this, I am posting an image of something funny that happened (or, at least, I didn´t expect it):

When asking for Null on a Null Point, it returns false (!?). I had to ask for NaN on her coordinates to return true, as Giulio suggested in his attachment.


I suppose it has to do with the fact that it comes from an intersection event, as David says??

I posted the definition as well, in case you want to have a look at it.

Cheers
Attachments:
There are no null with Point3d's... this is because ValueTypes cannot be null (beware of Nothing in Vb.Net, too: Point3d(0,0,0) equals Nothing in Vb.Net).

There is a special Point3d.Unset and Point3d(double.NaN, double.NaN, double.NaN);

NaN stands for "Not A Number". A single NaN will invalidate the point.
Point3d p = new Point3d(double.NaN, 0, 0);
bool valid = p.IsValid;
A = valid; //A is false

the property Point3d.IsValid (I used it somewhere in the script above) checks against all these.

I hope it helps,

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
Ok, Giulio, now I understand it.

I think Point3d.Unset is what I was looking for.

Thanks!
There are some limitations in Point3d.Unset, therefore I'd still use double.NaN if possible.

The limitation is basically that Point3d.Unset - Point3d.Unset gives Point3d(0,0,0). Unset is actually just a very large negative number, about -10^308.

On the other end, Point3d(NaN, NaN, NaN) - Point3d(NaN, NaN, NaN) returns faithfully Point3d(NaN, NaN, NaN).

So please use Point3d.Unset but keep this possible error into account.

- Giulio
_______________
giulio@mcneel.com
McNeel Europe, Barcelona
Yeah, I actually wasn't a fan about this decision. There's a reason we don't use double.NaN in RhinoCommon, but I don't think it's a good enough reason to abandon the standard NaN convention in .NET and come up with our own.

--
David Rutten
david@mcneel.com
Seattle, WA

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service