23#ifdef ENABLE_OPENCASCADE
25#ifdef __INTEL_COMPILER
26#pragma warning disable 191
32#undef _USE_MATH_DEFINES
41#include <BRep_Tool.hxx>
42#include <Poly_Array1OfTriangle.hxx>
43#include <Poly_Triangulation.hxx>
44#include <Standard_Version.hxx>
45#include <TColgp_Array1OfPnt.hxx>
46#include <TopExp_Explorer.hxx>
48#include <TopoDS_Face.hxx>
49#include <TopoDS_Shape.hxx>
54#ifdef __INTEL_COMPILER
55#pragma warning enable 191
59auto getNode(
const Handle(Poly_Triangulation) facing, Standard_Integer i) {
60#if OCC_VERSION_MAJOR >= 7 && OCC_VERSION_MINOR >= 6
61 return facing->Node(i);
64 TColgp_Array1OfPnt tab(1, (facing->NbNodes()));
65 tab = facing->Nodes();
71auto getTriangle(
const Handle(Poly_Triangulation) facing, Standard_Integer i) {
72#if OCC_VERSION_MAJOR >= 7 && OCC_VERSION_MINOR >= 6
73 return facing->Triangle(i);
76 Poly_Array1OfTriangle tri(1, facing->NbTriangles());
77 tri = facing->Triangles();
91void render(
const IObjComponent &ObjComp) {
93 V3D pos = ObjComp.getPos();
94 Quat rot = ObjComp.getRotation();
96 rot.GLMatrix(&rotGL[0]);
97 glTranslated(pos[0], pos[1], pos[2]);
99 V3D scaleFactor = ObjComp.getScaleFactor();
100 glScaled(scaleFactor[0], scaleFactor[1], scaleFactor[2]);
101 ObjComp.drawObject();
106void render(detail::GeometryTriangulator &triangulator) {
107 const auto &faces = triangulator.getTriangleFaces();
108 const auto &points = triangulator.getTriangleVertices();
109 glBegin(GL_TRIANGLES);
110 for (
size_t i = 0; i < triangulator.numTriangleFaces(); i++) {
111 auto index2 =
static_cast<size_t>(faces[i * 3 + 1] * 3);
112 auto index3 =
static_cast<size_t>(faces[i * 3 + 2] * 3);
113 auto index1 =
static_cast<size_t>(faces[i * 3] * 3);
115 const V3D v1(points[index1], points[index1 + 1], points[index1 + 2]);
116 const V3D v2(points[index2], points[index2 + 1], points[index2 + 2]);
117 const V3D v3(points[index3], points[index3 + 1], points[index3 + 2]);
118 const auto normal =
normalize((v1 - v2).cross_prod(v2 - v3));
119 glNormal3d(normal[0], normal[1], normal[2]);
120 glVertex3dv(&points[index1]);
121 glVertex3dv(&points[index2]);
122 glVertex3dv(&points[index3]);
127#ifdef ENABLE_OPENCASCADE
129void render(
const TopoDS_Shape &ObjSurf) {
130 glBegin(GL_TRIANGLES);
131 if (!ObjSurf.IsNull()) {
133 for (Ex.Init(ObjSurf, TopAbs_FACE); Ex.More(); Ex.Next()) {
134 TopoDS_Face F = TopoDS::Face(Ex.Current());
136 Handle(Poly_Triangulation) facing = BRep_Tool::Triangulation(F, L);
138 for (Standard_Integer i = 1; i <= (facing->NbTriangles()); i++) {
139 const Poly_Triangle trian = getTriangle(facing, i);
140 Standard_Integer index1, index2, index3;
141 trian.Get(index1, index2, index3);
142 gp_Pnt point1 = getNode(facing, index1);
143 gp_Pnt point2 = getNode(facing, index2);
144 gp_Pnt point3 = getNode(facing, index3);
145 gp_XYZ pt1 = point1.XYZ();
146 gp_XYZ pt2 = point2.XYZ();
147 gp_XYZ pt3 = point3.XYZ();
149 gp_XYZ v1 = pt2 - pt1;
150 gp_XYZ v2 = pt3 - pt2;
152 gp_XYZ normal = v1 ^ v2;
154 glNormal3d(normal.X(), normal.Y(), normal.Z());
155 glVertex3d(point1.X(), point1.Y(), point1.Z());
156 glVertex3d(point2.X(), point2.Y(), point2.Z());
157 glVertex3d(point3.X(), point3.Y(), point3.Z());
165void renderSphere(
const detail::ShapeInfo &shapeInfo) {
167 GLUquadricObj *qobj = gluNewQuadric();
168 gluQuadricDrawStyle(qobj, GLU_FILL);
169 gluQuadricNormals(qobj, GL_SMOOTH);
171 auto center = shapeInfo.points()[0];
172 glTranslated(center[0], center[1], center[2]);
175 gluDeleteQuadric(qobj);
178void renderCuboid(
const detail::ShapeInfo &shapeInfo) {
179 const auto &points = shapeInfo.points();
180 V3D vec0 = points[0];
181 V3D vec1 = points[1] - points[0];
182 V3D vec2 = points[2] - points[0];
183 V3D vec3 = points[3] - points[0];
186 vertex[1] = vec0 + vec3;
187 vertex[2] = vec0 + vec3 + vec1;
188 vertex[3] = vec0 + vec1;
189 vertex[4] = vec0 + vec2;
190 vertex[5] = vec0 + vec2 + vec3;
191 vertex[6] = vec0 + vec2 + vec3 + vec1;
192 vertex[7] = vec0 + vec1 + vec2;
194 int faceindex[6][4] = {
204 for (
auto &row : faceindex) {
205 const auto normal =
normalize((vertex[row[0]] - vertex[row[1]]).cross_prod((vertex[row[0]] - vertex[row[2]])));
206 glNormal3d(normal[0], normal[1], normal[2]);
207 for (
const int ij : row) {
224 glVertex3d(vertex[ij][0], vertex[ij][1], vertex[ij][2]);
230void renderHexahedron(
const detail::ShapeInfo &shapeInfo) {
232 const auto &points = shapeInfo.points();
234 glVertex3d(points[0].
X(), points[0].
Y(), points[0].
Z());
235 glVertex3d(points[1].
X(), points[1].
Y(), points[1].
Z());
236 glVertex3d(points[2].
X(), points[2].
Y(), points[2].
Z());
237 glVertex3d(points[3].
X(), points[3].
Y(), points[3].
Z());
239 glVertex3d(points[1].
X(), points[1].
Y(), points[1].
Z());
240 glVertex3d(points[5].
X(), points[5].
Y(), points[5].
Z());
241 glVertex3d(points[6].
X(), points[6].
Y(), points[6].
Z());
242 glVertex3d(points[2].
X(), points[2].
Y(), points[2].
Z());
244 glVertex3d(points[2].
X(), points[2].
Y(), points[2].
Z());
245 glVertex3d(points[6].
X(), points[6].
Y(), points[6].
Z());
246 glVertex3d(points[7].
X(), points[7].
Y(), points[7].
Z());
247 glVertex3d(points[3].
X(), points[3].
Y(), points[3].
Z());
249 glVertex3d(points[3].
X(), points[3].
Y(), points[3].
Z());
250 glVertex3d(points[7].
X(), points[7].
Y(), points[7].
Z());
251 glVertex3d(points[4].
X(), points[4].
Y(), points[4].
Z());
252 glVertex3d(points[0].
X(), points[0].
Y(), points[0].
Z());
254 glVertex3d(points[0].
X(), points[0].
Y(), points[0].
Z());
255 glVertex3d(points[4].
X(), points[4].
Y(), points[4].
Z());
256 glVertex3d(points[5].
X(), points[5].
Y(), points[5].
Z());
257 glVertex3d(points[1].
X(), points[1].
Y(), points[1].
Z());
259 glVertex3d(points[4].
X(), points[4].
Y(), points[4].
Z());
260 glVertex3d(points[5].
X(), points[5].
Y(), points[5].
Z());
261 glVertex3d(points[6].
X(), points[6].
Y(), points[6].
Z());
262 glVertex3d(points[7].
X(), points[7].
Y(), points[7].
Z());
267void renderCone(
const detail::ShapeInfo &shapeInfo) {
269 GLUquadricObj *qobj = gluNewQuadric();
270 gluQuadricDrawStyle(qobj, GLU_FILL);
271 gluQuadricNormals(qobj, GL_SMOOTH);
272 auto center = shapeInfo.points()[0];
273 glTranslated(center[0], center[1], center[2]);
276 auto axis = shapeInfo.points()[1];
277 Quat rot(unit, axis);
278 rot.GLMatrix(&mat[0]);
280 auto radius = shapeInfo.radius();
281 auto height = shapeInfo.height();
283 glTranslated(0.0, 0.0,
height);
288void renderCylinder(
const detail::ShapeInfo &shapeInfo) {
289 GLUquadricObj *qobj = gluNewQuadric();
290 gluQuadricDrawStyle(qobj, GLU_FILL);
291 gluQuadricNormals(qobj, GL_SMOOTH);
292 gluQuadricTexture(qobj,
true);
294 auto center = shapeInfo.points()[0];
295 glTranslated(center[0], center[1], center[2]);
298 auto axis = shapeInfo.points()[1];
299 Quat rot(unit, axis);
300 rot.GLMatrix(&mat[0]);
302 auto radius = shapeInfo.radius();
303 auto height = shapeInfo.height();
305 gluQuadricTexture(qobj,
false);
307 glTranslated(0.0, 0.0,
height);
313namespace RenderingHelpers {
317#ifdef ENABLE_OPENCASCADE
318 if (triangulator.hasOCSurface() && !triangulator.getOCSurface().IsNull())
319 render(triangulator.getOCSurface());
321 render(triangulator);
323 render(triangulator);
328 switch (shapeInfo.
shape()) {
330 renderCuboid(shapeInfo);
333 renderSphere(shapeInfo);
336 renderHexahedron(shapeInfo);
339 renderCone(shapeInfo);
342 renderCylinder(shapeInfo);
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
static constexpr int g_NSLICES
The number of slices to approximate a cone.
static constexpr int g_NSTACKS
The number of stacks to approximate a cone.
static constexpr int g_NSTACKS
The number of stacks to approximate a cylinder.
static constexpr int g_NSLICES
The number of slices to approximate a cylinder.
Object Component class, this class brings together the physical attributes of the component to the po...
static constexpr int g_NSLICES
The number of slices to approximate a sphere.
static constexpr int g_NSTACKS
The number of stacks to approximate a sphere.
GeometryTriangulator : Triangulates object surfaces.
GeometryShape shape() const
MANTID_GEOMETRY_DLL void renderIObjComponent(const IObjComponent &objComp)
Render IObjComponent.
MANTID_GEOMETRY_DLL void renderTriangulated(detail::GeometryTriangulator &triangulator)
Render Traingulated Surface.
MANTID_GEOMETRY_DLL void renderShape(const detail::ShapeInfo &shapeInfo)
Renders a sphere, cuboid, hexahedron, cone or cylinder.
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.