Re: [osg-users] How to return a NULL value of osg::Vec3f type
Hi, Thank you so much for your answers and you helped me a lot. But I don't know how to mark this question as "solved" because I am newbie here. I raised this question because in the beginning I thought the osg::Vec3f is constructed using std::vector template (it seems that I was wrong:( ). So I was wondering why it cannot return NULL as vector did. Next I think I need to read the source code of osg to better understand the underlying stuff. Thank you! Cheers, Yong -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=55818#55818 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to return a NULL value of osg::Vec3f type
Hi Yong, The simple answer is you can't convert a NULL to Vec3, it simply is wrong and compiler is right to bark about it. The best way to write code like is would be to do: bool lineintersection(const osg::Vec3f& p1, const osg::Vec3f& p2, const osg::Vec3f &p3, const osg::Vec3f& p4, osg::Vec3f& intersectionPoint) { float x1 = p1.x(), x2 = p2.x(), x3 = p3.x(), x4 = p4.x(); float y1 = p1.y(), y2 = p2.y(), y3 = p3. y(), y4 = p4.y(); float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); if(d==0) return false; float p = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4); float x = ( p * (x3 - x4) - (x1 - x2) * post ) / d; float y = ( p * (y3 - y4) - (y1 - y2) * post ) / d; intersectionPoint.set(x, y, 0.0f); return true; } Robert. On 13 May 2013 05:10, Yong Chen wrote: > Hi, > > The following lines are used to calculate the intersection of two lines in a > 2D plane. However, the NULL value cannot be returned because the Visual > Studio tells me that "error C2664: cannot convert parameter 1 from 'int' to > 'const osg::Vec3f &'". I don't know how to represent a Null value here. Very > appreciate if you guys can help me out. > > osg::Vec3f lineintersection(osg::Vec3f p1, osg::Vec3f p2, osg::Vec3f p3, > osg::Vec3f p4) > { > float x1 = p1.x(), x2 = p2.x(), x3 = p3.x(), x4 = p4.x(); > float y1 = p1.y(), y2 = p2.y(), y3 = p3. y(), y4 = p4.y(); > > float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); > if(d==0) return NULL; // error occurs here > > float p = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4); > float x = ( p * (x3 - x4) - (x1 - x2) * post ) / d; > float y = ( p * (y3 - y4) - (y1 - y2) * post ) / d; > > return osg::Vec3f(x, y, 0.0f); > } > > Thank you! > > Cheers, > Yong > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=53991#53991 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to return a NULL value of osg::Vec3f type
This is more of a c++/architecture question. Here's something you could try: bool getLineIntersctionIfExists(osg::Vec3f p1, osg::Vec3f p2, osg::Vec3f p3, osg::Vec3f p4, osg::Vec3f& outIntersctionPoint) const { outIntersctionPoint = osg::Vec3f( 0.0f, 0.0f, 0.0f ); float x1 = p1.x(), x2 = p2.x(), x3 = p3.x(), x4 = p4.x(); float y1 = p1.y(), y2 = p2.y(), y3 = p3. y(), y4 = p4.y(); float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); if(d==0) return false; float p = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4); float x = ( p * (x3 - x4) - (x1 - x2) * post ) / d; float y = ( p * (y3 - y4) - (y1 - y2) * post ) / d; outIntersctionPoint = osg::Vec3f(x, y, 0.0f); return true; } The return value tells you if your lines are parallel or not, and if they are not, the intersection point is placed in outIntersctionPoint. This method could be static. Also, you should pay good attention to your d==0 comparison, as floating point values comparison are not really exact. You should seek an 'approximately equal' method out there. -- Alexandre Vaillancourt 2013/5/13 Yong Chen > Hi, > > The following lines are used to calculate the intersection of two lines in > a 2D plane. However, the NULL value cannot be returned because the Visual > Studio tells me that "error C2664: cannot convert parameter 1 from 'int' to > 'const osg::Vec3f &'". I don't know how to represent a Null value here. > Very appreciate if you guys can help me out. > > osg::Vec3f lineintersection(osg::Vec3f p1, osg::Vec3f p2, osg::Vec3f p3, > osg::Vec3f p4) > { > float x1 = p1.x(), x2 = p2.x(), x3 = p3.x(), x4 = p4.x(); > float y1 = p1.y(), y2 = p2.y(), y3 = p3. y(), y4 = p4.y(); > > float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); > if(d==0) return NULL; // error occurs here > > float p = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4); > float x = ( p * (x3 - x4) - (x1 - x2) * post ) / d; > float y = ( p * (y3 - y4) - (y1 - y2) * post ) / d; > > return osg::Vec3f(x, y, 0.0f); > } > > Thank you! > > Cheers, > Yong > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=53991#53991 > > > > > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] How to return a NULL value of osg::Vec3f type
Hi, The following lines are used to calculate the intersection of two lines in a 2D plane. However, the NULL value cannot be returned because the Visual Studio tells me that "error C2664: cannot convert parameter 1 from 'int' to 'const osg::Vec3f &'". I don't know how to represent a Null value here. Very appreciate if you guys can help me out. osg::Vec3f lineintersection(osg::Vec3f p1, osg::Vec3f p2, osg::Vec3f p3, osg::Vec3f p4) { float x1 = p1.x(), x2 = p2.x(), x3 = p3.x(), x4 = p4.x(); float y1 = p1.y(), y2 = p2.y(), y3 = p3. y(), y4 = p4.y(); float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); if(d==0) return NULL; // error occurs here float p = (x1*y2 - y1*x2), post = (x3*y4 - y3*x4); float x = ( p * (x3 - x4) - (x1 - x2) * post ) / d; float y = ( p * (y3 - y4) - (y1 - y2) * post ) / d; return osg::Vec3f(x, y, 0.0f); } Thank you! Cheers, Yong -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=53991#53991 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org