CacheGeometryHandler.cpp 4.22 KB
Newer Older
1
#include "MantidGeometry/Objects/CSGObject.h"
Peterson, Peter's avatar
Peterson, Peter committed
2
3
4
5
6
#include "MantidGeometry/Instrument/ObjComponent.h"
#include "MantidGeometry/Rendering/GeometryHandler.h"
#include "MantidGeometry/Rendering/CacheGeometryHandler.h"
#include "MantidGeometry/Rendering/CacheGeometryRenderer.h"
#include "MantidGeometry/Rendering/CacheGeometryGenerator.h"
7
#include "MantidKernel/MultiThreaded.h"
Peterson, Peter's avatar
Peterson, Peter committed
8

9
10
#include <boost/make_shared.hpp>

11
12
13
14
15
namespace Mantid {
namespace Geometry {

CacheGeometryHandler::CacheGeometryHandler(IObjComponent *comp)
    : GeometryHandler(comp) {
16
  Triangulator = nullptr;
17
18
19
  Renderer = new CacheGeometryRenderer();
}

20
CacheGeometryHandler::CacheGeometryHandler(boost::shared_ptr<CSGObject> obj)
21
22
23
24
25
    : GeometryHandler(obj) {
  Triangulator = new CacheGeometryGenerator(obj.get());
  Renderer = new CacheGeometryRenderer();
}

26
CacheGeometryHandler::CacheGeometryHandler(CSGObject *obj) : GeometryHandler(obj) {
27
28
29
30
31
32
33
34
35
36
  Triangulator = new CacheGeometryGenerator(obj);
  Renderer = new CacheGeometryRenderer();
}

boost::shared_ptr<GeometryHandler> CacheGeometryHandler::clone() const {
  auto clone = boost::make_shared<CacheGeometryHandler>(*this);
  clone->Renderer = new CacheGeometryRenderer(*(this->Renderer));
  if (this->Triangulator)
    clone->Triangulator = new CacheGeometryGenerator(this->Obj);
  else
37
    clone->Triangulator = nullptr;
38
39
40
41
  return clone;
}

CacheGeometryHandler::~CacheGeometryHandler() {
42
  if (Triangulator != nullptr)
43
    delete Triangulator;
44
  if (Renderer != nullptr)
45
46
47
48
49
50
51
52
    delete Renderer;
}

GeometryHandler *CacheGeometryHandler::createInstance(IObjComponent *comp) {
  return new CacheGeometryHandler(comp);
}

GeometryHandler *
53
CacheGeometryHandler::createInstance(boost::shared_ptr<CSGObject> obj) {
54
55
56
  return new CacheGeometryHandler(obj);
}

57
GeometryHandler *CacheGeometryHandler::createInstance(CSGObject *obj) {
58
59
60
61
62
63
  return new CacheGeometryHandler(obj);
}

void CacheGeometryHandler::Triangulate() {
  // Check whether Object is triangulated otherwise triangulate
  PARALLEL_CRITICAL(Triangulate)
64
  if (Obj != nullptr && !boolTriangulated) {
65
66
67
68
69
70
    Triangulator->Generate();
    boolTriangulated = true;
  }
}

void CacheGeometryHandler::Render() {
71
  if (Obj != nullptr) {
72
    if (!boolTriangulated)
73
74
75
76
      Triangulate();
    Renderer->Render(
        Triangulator->getNumberOfPoints(), Triangulator->getNumberOfTriangles(),
        Triangulator->getTriangleVertices(), Triangulator->getTriangleFaces());
77
  } else if (ObjComp != nullptr) {
78
    Renderer->Render(ObjComp);
79
  }
Peterson, Peter's avatar
Peterson, Peter committed
80
}
81
82

void CacheGeometryHandler::Initialize() {
83
  if (Obj != nullptr) {
84
    Obj->updateGeometryHandler();
85
    if (!boolTriangulated)
86
87
88
89
      Triangulate();
    Renderer->Initialize(
        Triangulator->getNumberOfPoints(), Triangulator->getNumberOfTriangles(),
        Triangulator->getTriangleVertices(), Triangulator->getTriangleFaces());
90
  } else if (ObjComp != nullptr) {
91
92
93
94
95
    Renderer->Initialize(ObjComp);
  }
}

int CacheGeometryHandler::NumberOfTriangles() {
96
  if (Obj != nullptr) {
97
    Obj->updateGeometryHandler();
98
    if (!boolTriangulated)
99
100
101
102
103
104
105
106
      Triangulate();
    return Triangulator->getNumberOfTriangles();
  } else {
    return 0;
  }
}

int CacheGeometryHandler::NumberOfPoints() {
107
  if (Obj != nullptr) {
108
    Obj->updateGeometryHandler();
109
    if (!boolTriangulated)
110
111
112
113
114
115
116
117
      Triangulate();
    return Triangulator->getNumberOfPoints();
  } else {
    return 0;
  }
}

double *CacheGeometryHandler::getTriangleVertices() {
118
  if (Obj != nullptr) {
119
    Obj->updateGeometryHandler();
120
    if (!boolTriangulated)
121
122
123
      Triangulate();
    return Triangulator->getTriangleVertices();
  } else {
124
    return nullptr;
125
126
127
128
  }
}

int *CacheGeometryHandler::getTriangleFaces() {
129
  if (Obj != nullptr) {
130
    Obj->updateGeometryHandler();
131
    if (!boolTriangulated)
132
133
134
      Triangulate();
    return Triangulator->getTriangleFaces();
  } else {
135
    return nullptr;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  }
}

/**
Sets the geometry cache using the triangulation information provided
@param noPts :: the number of points
@param noFaces :: the number of faces
@param pts :: a double array of the points
@param faces :: an int array of the faces
*/
void CacheGeometryHandler::setGeometryCache(int noPts, int noFaces, double *pts,
                                            int *faces) {
  Triangulator->setGeometryCache(noPts, noFaces, pts, faces);
  boolTriangulated = true;
}
}
}