• OSG笔记:对线求交失败


    问题描述

    使用以下两个求交器,对一根直线进行求交,但始终得不出结果。
    直线求交器 osgUtil::LineSegmentIntersector
    多面体求交器 osgUtil::PolytopeIntersector

    测试代码如下:

    //创建直线
    void CreateModel(osg::ref_ptr<osg::Geode> pGeode)
    {
    	if (pGeode) return;
    
    	osg::Vec3dArray* pVertexArray = new osg::Vec3dArray();
    	pVertexArray->push_back(osg::Vec3d(0, 0, 0));
    	pVertexArray->push_back(osg::Vec3d(100, 0, 0));
    
    	osg::Geometry* pLineGeom = new osg::Geometry();
    	pLineGeom->setVertexArray(pVertexArray);
    	pLineGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,
    		0, pVertexArray->size()));
    
    	pGeode->addDrawable(pLineGeom);
    }
    
    //多面体求交器的测试
    void TestPolytopeIntersector(osg::Camera* pCamera )
    {
    	if (!pCamera)
    	{
    		return;
    	}
    
    	osg::Vec3d ptCenter(50, 0, 0);
    
    	//构建多面体求交器
    	osg::ref_ptr<osgUtil::PolytopeIntersector> pPolyIntersector;
    	osg::BoundingBox mBoundBox(
    		ptCenter.x() - 51.0,
    		ptCenter.y() - 51.0,
    		ptCenter.z() - 51.0,
    		ptCenter.x() + 51.0,
    		ptCenter.y() + 51.0,
    		ptCenter.z() + 51.0);
    	osg::Polytope mPolytope;
    	mPolytope.setToBoundingBox(mBoundBox);
    	pPolyIntersector = new osgUtil::PolytopeIntersector(
    		osgUtil::Intersector::CoordinateFrame::MODEL, mPolytope);
    
    	//优化求交器
    	pPolyIntersector->setIntersectionLimit(
    		osgUtil::Intersector::IntersectionLimit::LIMIT_ONE_PER_DRAWABLE);
    
    	//执行求交
    	osgUtil::IntersectionVisitor intersectionVisitor;
    	intersectionVisitor.setIntersector(pPolyIntersector.get());
    	pCamera->accept(intersectionVisitor);
    
    	//构造求交结果
    	if (pPolyIntersector->containsIntersections())
    	{
    		osgUtil::PolytopeIntersector::Intersections& mIntersection = pPolyIntersector->getIntersections();
    		osgUtil::PolytopeIntersector::Intersections::iterator itr;
    		for (itr = mIntersection.begin();
    			itr != mIntersection.end();
    			++itr)
    		{
    			osgUtil::PolytopeIntersector::Intersection mSingleIter = (*itr);
    			osg::NodePath& nodePath = mSingleIter.nodePath;
    
    			//循环判断。
    			int iCount = nodePath.size();
    			for (int i = iCount - 1; i >= 0; --i)
    			{
    				osg::ref_ptr<osg::Node> node = nodePath[i];
    
    				//处理省略...
    			}
    
    		}
    	}
    }
    
    //直线求线器的测试
    void TestLineSegmentIntersector(osg::Camera* pCamera)
    {
    	if (!pCamera)
    	{
    		return;
    	}
    
    	osg::ref_ptr<osgUtil::LineSegmentIntersector> pLineIntersector;
    	pLineIntersector = new osgUtil::LineSegmentIntersector(
    		osgUtil::Intersector::CoordinateFrame::MODEL,
    		osg::Vec3d(10, 0, 0),
    		osg::Vec3d(10, 1, 1));
    
    	//执行求交
    	osgUtil::IntersectionVisitor intersectionVisitor;
    	intersectionVisitor.setIntersector(pLineIntersector.get());
    	pCamera->accept(intersectionVisitor);
    
    	//构造求交结果
    	if (pLineIntersector->containsIntersections())
    	{
    		osgUtil::LineSegmentIntersector::Intersections& mIntersection = pLineIntersector->getIntersections();
    		osgUtil::LineSegmentIntersector::Intersections::iterator itr;
    		for (itr = mIntersection.begin();
    			itr != mIntersection.end();
    			++itr)
    		{
    			osgUtil::LineSegmentIntersector::Intersection mSingleIter = (*itr);
    			osg::NodePath& nodePath = mSingleIter.nodePath;
    			int iCount = nodePath.size();
    			for (int i = iCount - 1; i >= 0; --i)
    			{
    				osg::ref_ptr<osg::Node> node = nodePath[i];
    				//处理省略...
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    osg代码的探索

    查看osg的代码后,发现以下事实:

    • osgUtil::LineSegmentIntersector使用了osg::TriangleFunctor。
      从osg::TriangleFunctor::setVertexArray可知:不支持osg::Vec2d、osg::Vec3d、osg::Vec4d等等,支持osg::Vec3(即osg::Vec3f)
      从osg::TriangleFunctor::drawArrays可知:不支持GL_POINTS、GL_LINES、GL_LINE_STRIP、GL_LINE_LOOP

    • osgUtil::PolytopeIntersector使用了osg::TemplatePrimitiveFunctor。
      从osg::TemplatePrimitiveFunctor::setVertexArray可知:不支持osg::Vec2d、osg::Vec3d、osg::Vec4d等等,支持osg::Vec3(即osg::Vec3f)
      从osg::TemplatePrimitiveFunctor::drawArrays可知:支持所有的基本图元类型。

    osg代码整理如下:

    
    
    //多面体求交器相关代码
    //
    void PolytopeIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)
    {
    	//
    	//其他代码省略....
    	//
    
    	osg::TemplatePrimitiveFunctor<PolytopeIntersectorUtils::PolytopePrimitiveIntersector> func;
    	func.setPolytope(_polytope, _referencePlane);
    	func.setDimensionMask(_dimensionMask);
    	func.setLimitOneIntersection(_intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE);
    
    	drawable->accept(func);
    
    	//
    	//其他代码省略....
    	//
    }
    
    
    /** Provides access to the primitives that compose an \c osg::Drawable.
    *  

    Notice that \c TemplatePrimitiveFunctor is a class template, and that it inherits * from its template parameter \c T. This template parameter must implement * operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 * v3, bool treatVertexDataAsTemporary), * operator()(const osg::Vec3 v1, const osg::Vec3 v2, bool * treatVertexDataAsTemporary), operator()(const osg::Vec3 v1, * const osg::Vec3 v2, const osg::Vec3 v3, bool treatVertexDataAsTemporary), * and operator()(const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 v3, * const osg::Vec3 v4, bool treatVertexDataAsTemporary) which will be called * for the matching primitive when the functor is applied to a \c Drawable. * Parameters \c v1, \c v2, \c v3, and \c v4 are the vertices of the primitive. * The last parameter, \c treatVertexDataAsTemporary, indicates whether these * vertices are coming from a "real" vertex array, or from a temporary vertex array, * created by the \c TemplatePrimitiveFunctor from some other geometry representation. * @see \c PrimitiveFunctor for general usage hints. */ template<class T> class TemplatePrimitiveFunctor : public PrimitiveFunctor, public T { public: // //其他代码省略.... // virtual void setVertexArray(unsigned int, const Vec2*) { notify(WARN) << "Triangle Functor does not support Vec2* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int count, const Vec3* vertices) { _vertexArraySize = count; _vertexArrayPtr = vertices; } virtual void setVertexArray(unsigned int, const Vec4*) { notify(WARN) << "Triangle Functor does not support Vec4* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int, const Vec2d*) { notify(WARN) << "Triangle Functor does not support Vec2d* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int, const Vec3d*) { notify(WARN) << "Triangle Functor does not support Vec3d* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int, const Vec4d*) { notify(WARN) << "Triangle Functor does not support Vec4d* vertex arrays" << std::endl; } virtual void drawArrays(GLenum mode, GLint first, GLsizei count) { if (_vertexArrayPtr == 0 || count == 0) return; switch (mode) { case(GL_TRIANGLES) : { const Vec3* vlast = &_vertexArrayPtr[first + count]; for (const Vec3* vptr = &_vertexArrayPtr[first]; vptr < vlast; vptr += 3) this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), _treatVertexDataAsTemporary); break; } case(GL_TRIANGLE_STRIP) : { const Vec3* vptr = &_vertexArrayPtr[first]; for (GLsizei i = 2; i < count; ++i, ++vptr) { if ((i % 2)) this->operator()(*(vptr), *(vptr + 2), *(vptr + 1), _treatVertexDataAsTemporary); else this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), _treatVertexDataAsTemporary); } break; } case(GL_QUADS) : { const Vec3* vptr = &_vertexArrayPtr[first]; for (GLsizei i = 3; i < count; i += 4, vptr += 4) { this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), *(vptr + 3), _treatVertexDataAsTemporary); } break; } case(GL_QUAD_STRIP) : { const Vec3* vptr = &_vertexArrayPtr[first]; for (GLsizei i = 3; i < count; i += 2, vptr += 2) { this->operator()(*(vptr), *(vptr + 1), *(vptr + 3), *(vptr + 2), _treatVertexDataAsTemporary); } break; } case(GL_POLYGON) : // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN) : { const Vec3* vfirst = &_vertexArrayPtr[first]; const Vec3* vptr = vfirst + 1; for (GLsizei i = 2; i < count; ++i, ++vptr) { this->operator()(*(vfirst), *(vptr), *(vptr + 1), _treatVertexDataAsTemporary); } break; } case(GL_POINTS) : { const Vec3* vlast = &_vertexArrayPtr[first + count]; for (const Vec3* vptr = &_vertexArrayPtr[first]; vptr < vlast; vptr += 1) this->operator()(*(vptr), _treatVertexDataAsTemporary); break; } case(GL_LINES) : { const Vec3* vlast = &_vertexArrayPtr[first + count - 1]; for (const Vec3* vptr = &_vertexArrayPtr[first]; vptr < vlast; vptr += 2) this->operator()(*(vptr), *(vptr + 1), _treatVertexDataAsTemporary); break; } case(GL_LINE_STRIP) : { const Vec3* vlast = &_vertexArrayPtr[first + count - 1]; for (const Vec3* vptr = &_vertexArrayPtr[first]; vptr < vlast; vptr += 1) this->operator()(*(vptr), *(vptr + 1), _treatVertexDataAsTemporary); break; } case(GL_LINE_LOOP) : { const Vec3* vlast = &_vertexArrayPtr[first + count - 1]; for (const Vec3* vptr = &_vertexArrayPtr[first]; vptr < vlast; vptr += 1) this->operator()(*(vptr), *(vptr + 1), _treatVertexDataAsTemporary); this->operator()(*(vlast), _vertexArrayPtr[first], _treatVertexDataAsTemporary); break; } default: break; } } // //其他代码省略.... // }; //线性求交器相关代码 // void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable) { // //其他代码省略.... // osg::KdTree* kdTree = iv.getUseKdTreeWhenAvailable() ? dynamic_cast<osg::KdTree*>(drawable->getShape()) : 0; if (kdTree) //此处是其他逻辑,没研究了 { // //其他代码省略.... // return; } // //其他代码省略.... // osg::TriangleFunctor<LineSegmentIntersectorUtils::TriangleIntersector> ti; ti.set(s, e); ti._limitOneIntersection = (_intersectionLimit == LIMIT_ONE_PER_DRAWABLE || _intersectionLimit == LIMIT_ONE); drawable->accept(ti); // //其他代码省略.... // } /** Provides access to the triangles that compose an \c osg::Drawable. If the \c * Drawable is not composed of triangles, the \c TriangleFunctor will convert * the primitives to triangles whenever possible. *

    Notice that \c TriangleFunctor is a class template, and that it inherits * from its template parameter \c T. This template parameter must implement * T::operator() (const osg::Vec3 v1, const osg::Vec3 v2, const osg::Vec3 * v3, bool treatVertexDataAsTemporary), which will be called for every * triangle when the functor is applied to a \c Drawable. Parameters \c v1, \c * v2, and \c v3 are the triangle vertices. The fourth parameter, \c * treatVertexDataAsTemporary, indicates whether these vertices are coming from * a "real" vertex array, or from a temporary vertex array, created by the \c * TriangleFunctor from some other geometry representation. * @see \c PrimitiveFunctor for general usage hints. */ template<class T> class TriangleFunctor : public PrimitiveFunctor, public T { public: // //其他代码省略.... // virtual void setVertexArray(unsigned int, const Vec2*) { notify(WARN) << "Triangle Functor does not support Vec2* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int count, const Vec3* vertices) { _vertexArraySize = count; _vertexArrayPtr = vertices; } virtual void setVertexArray(unsigned int, const Vec4*) { notify(WARN) << "Triangle Functor does not support Vec4* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int, const Vec2d*) { notify(WARN) << "Triangle Functor does not support Vec2d* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int, const Vec3d*) { notify(WARN) << "Triangle Functor does not support Vec3d* vertex arrays" << std::endl; } virtual void setVertexArray(unsigned int, const Vec4d*) { notify(WARN) << "Triangle Functor does not support Vec4d* vertex arrays" << std::endl; } virtual void drawArrays(GLenum mode, GLint first, GLsizei count) { if (_vertexArrayPtr == 0 || count == 0) return; switch (mode) { case(GL_TRIANGLES) : { const Vec3* vlast = &_vertexArrayPtr[first + count]; for (const Vec3* vptr = &_vertexArrayPtr[first]; vptr < vlast; vptr += 3) this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), _treatVertexDataAsTemporary); break; } case(GL_TRIANGLE_STRIP) : { const Vec3* vptr = &_vertexArrayPtr[first]; for (GLsizei i = 2; i < count; ++i, ++vptr) { if ((i % 2)) this->operator()(*(vptr), *(vptr + 2), *(vptr + 1), _treatVertexDataAsTemporary); else this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), _treatVertexDataAsTemporary); } break; } case(GL_QUADS) : { const Vec3* vptr = &_vertexArrayPtr[first]; for (GLsizei i = 3; i < count; i += 4, vptr += 4) { this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), _treatVertexDataAsTemporary); this->operator()(*(vptr), *(vptr + 2), *(vptr + 3), _treatVertexDataAsTemporary); } break; } case(GL_QUAD_STRIP) : { const Vec3* vptr = &_vertexArrayPtr[first]; for (GLsizei i = 3; i < count; i += 2, vptr += 2) { this->operator()(*(vptr), *(vptr + 1), *(vptr + 2), _treatVertexDataAsTemporary); this->operator()(*(vptr + 1), *(vptr + 3), *(vptr + 2), _treatVertexDataAsTemporary); } break; } case(GL_POLYGON) : // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN) : { const Vec3* vfirst = &_vertexArrayPtr[first]; const Vec3* vptr = vfirst + 1; for (GLsizei i = 2; i < count; ++i, ++vptr) { this->operator()(*(vfirst), *(vptr), *(vptr + 1), _treatVertexDataAsTemporary); } break; } case(GL_POINTS) : case(GL_LINES) : case(GL_LINE_STRIP) : case(GL_LINE_LOOP) : default: // can't be converted into to triangles. break; } } // //其他代码省略.... // };

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322

    解决方案

    由上可知,要对线进行求交,需使用osgUtil::PolytopeIntersector, 并且顶点数组一定要使用osg::Vec3(即osg::Vec3f)。

  • 相关阅读:
    Android Fragment 基本概念和基本使用
    信息检索(五):Query Expansion Using Contextual Clue Sampling with Language Models
    一言不合就重构
    Vue 全局事件总线$bus
    LeetCode每日一题(1621. Number of Sets of K Non-Overlapping Line Segments)
    Windows:Arduino IDE 开发环境配置【保姆级】
    springboot 数据字典设计思路:字典表+字典枚举 两者兼故方案,查询返回结果在controller方法上添加注解进行字典翻译
    堆栈认知——栈溢出实例(ret2libc)
    如果忘记了 iPhone 密码
    Mybatis之动态SQL
  • 原文地址:https://blog.csdn.net/s634772208/article/details/126975006