Skip to content

Commit a5c95a9

Browse files
committed
Can feed scirun trisurf to ospray
1 parent 0e75ee7 commit a5c95a9

File tree

1 file changed

+125
-25
lines changed

1 file changed

+125
-25
lines changed

src/Modules/Visualization/Tests/OsprayFieldRenderTests.cc

Lines changed: 125 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,45 @@
3030
#include <Modules/Visualization/ShowField.h>
3131
#include <Core/Datatypes/Legacy/Field/Field.h>
3232
#include <Core/Algorithms/Base/AlgorithmVariableNames.h>
33+
#include <Core/Algorithms/Legacy/Fields/ConvertMeshType/ConvertMeshToTriSurfMeshAlgo.h>
34+
#include <Core/Algorithms/Legacy/Fields/MeshDerivatives/GetFieldBoundaryAlgo.h>
3335
#include <Core/Utils/Exception.h>
3436
#include <Core/Logging/Log.h>
35-
#include <Core/Datatypes/ColorMap.h>
37+
#include <Core/Datatypes/Legacy/Field/Field.h>
38+
#include <Core/Datatypes/Legacy/Field/VMesh.h>
39+
#include <Core/Datatypes/Mesh/VirtualMeshFacade.h>
3640

3741
#include <ospray/ospray.h>
3842

43+
using namespace SCIRun;
3944
using namespace SCIRun::Testing;
4045
using namespace SCIRun::TestUtils;
4146
using namespace SCIRun::Core::Datatypes;
4247
using namespace SCIRun::Dataflow::Networks;
4348
using namespace SCIRun::Core::Algorithms;
49+
using namespace Fields;
4450
using namespace SCIRun::Core::Algorithms::Visualization;
4551
using namespace SCIRun::Modules::Visualization;
4652
using namespace SCIRun::Core;
47-
using namespace SCIRun;
4853
using namespace SCIRun::Core::Logging;
4954
using ::testing::Values;
5055
using ::testing::Combine;
5156
using ::testing::Range;
5257

5358
class OsprayFieldRenderTest : public ParameterizedModuleTest<int>
5459
{
60+
public:
61+
static void SetUpTestCase()
62+
{
63+
// initialize OSPRay; OSPRay parses (and removes) its commandline parameters, e.g. "--osp:debug"
64+
const char* argv[] = { "" };
65+
int argc = 0;
66+
int init_error = ospInit(&argc, argv);
67+
if (init_error != OSP_NO_ERROR)
68+
throw init_error;
69+
}
5570
protected:
56-
virtual void SetUp()
71+
virtual void SetUp() override
5772
{
5873
Log::get().setVerbose(false);
5974
auto size = GetParam();
@@ -92,39 +107,116 @@ namespace osprayImpl
92107
std::cout << "wrote file " << fileName << std::endl;
93108
}
94109

110+
template <typename T>
111+
std::string join(const T& list)
112+
{
113+
std::ostringstream oss;
114+
const auto SIZE = list.size();
115+
for (int i = 0; i < SIZE; ++i)
116+
{
117+
oss << list[i];
118+
if (i < SIZE - 1)
119+
oss << ", ";
120+
}
121+
return oss.str();
122+
}
95123

96-
int mainFunc()
124+
int renderLatVol(FieldHandle latvol, float cameraSteps)
97125
{
126+
if (false)
127+
{
128+
GetFieldBoundaryAlgo getfieldbound_algo;
129+
FieldHandle field_boundary;
130+
getfieldbound_algo.run(latvol, field_boundary);
131+
ConvertMeshToTriSurfMeshAlgo converter;
132+
FieldHandle trisurf;
133+
converter.run(field_boundary, trisurf);
134+
}
135+
136+
auto trisurf = TriangleTriSurfLinearBasis(DOUBLE_E);
137+
auto facade(trisurf->mesh()->getFacade());
138+
139+
//std::ostringstream ostr;
140+
//for (const auto& node : facade->nodes())
141+
//{
142+
// auto edges = node.edgeIndices();
143+
// ostr << "Node " << node.index() << " point=" << node.point().get_string() << /*" edges=[" << join(edges) << "]" <<*/ std::endl;
144+
//}
145+
//for (const auto& face : facade->faces())
146+
//{
147+
// auto faceID = face.index();
148+
// auto nodes = face.nodeIndices();
149+
// ostr << "Face " << faceID << " nodes=[" << join(nodes) << "]" << std::endl;
150+
//}
151+
152+
//std::cout << ostr.str() << std::endl;
153+
98154
std::cout << "hello ospray" << std::endl;
99155
// image size
100156
osp::vec2i imgSize;
101157
imgSize.x = 1024; // width
102158
imgSize.y = 768; // height
103159

104160
// camera
105-
float cam_pos[] = { 0.f, 0.f, 0.f };
161+
float cam_pos[] = { -1.f, 0.f, 1.f };
162+
cam_pos[0] += cameraSteps;
106163
float cam_up[] = { 0.f, 1.f, 0.f };
107-
float cam_view[] = { 0.1f, 0.f, 1.f };
164+
float cam_view[] = { 0.5f, 0.5f, -1.f };
165+
//cam_view[2] += cam_pos[2];
108166

109167
// triangle mesh data
110-
float vertex[] = { -1.0f, -1.0f, 3.0f, 0.f,
168+
float vertex_example[] = { -1.0f, -1.0f, 3.0f, 0.f,
111169
-1.0f, 1.0f, 3.0f, 0.f,
112170
1.0f, -1.0f, 3.0f, 0.f,
113171
0.1f, 0.1f, 0.3f, 0.f };
114-
float color[] = { 0.9f, 0.5f, 0.5f, 1.0f,
172+
173+
std::vector<float> vertex, color;
174+
{
175+
//auto i = 0;
176+
for (const auto& node : facade->nodes())
177+
{
178+
auto point = node.point();
179+
vertex.push_back(static_cast<float>(point.x()));
180+
vertex.push_back(static_cast<float>(point.y()));
181+
vertex.push_back(static_cast<float>(point.z()));
182+
vertex.push_back(0);
183+
//++i;
184+
//if (i % 2 == 0)
185+
{
186+
color.push_back(0.9f);
187+
color.push_back(0.5f);
188+
color.push_back(0.5f);
189+
}
190+
//else
191+
//{
192+
// color.push_back(0.8f);
193+
// color.push_back(0.8f);
194+
// color.push_back(0.8f);
195+
//}
196+
color.push_back(1.0f);
197+
}
198+
}
199+
200+
float color_example[] = { 0.9f, 0.5f, 0.5f, 1.0f,
115201
0.8f, 0.8f, 0.8f, 1.0f,
116202
0.8f, 0.8f, 0.8f, 1.0f,
117203
0.5f, 0.9f, 0.5f, 1.0f };
118-
int32_t index[] = { 0, 1, 2,
119-
1, 2, 3 };
204+
int32_t index_example[] = { 0, 1, 2,
205+
1, 2, 3};
206+
207+
std::vector<int32_t> index;
208+
{
209+
for (const auto& face : facade->faces())
210+
{
211+
auto nodes = face.nodeIndices();
212+
index.push_back(static_cast<int32_t>(nodes[0]));
213+
index.push_back(static_cast<int32_t>(nodes[1]));
214+
index.push_back(static_cast<int32_t>(nodes[2]));
215+
}
216+
}
120217

121218

122-
// initialize OSPRay; OSPRay parses (and removes) its commandline parameters, e.g. "--osp:debug"
123-
const char* argv[] = {""};
124-
int argc = 0;
125-
int init_error = ospInit(&argc, argv);
126-
if (init_error != OSP_NO_ERROR)
127-
return init_error;
219+
128220

129221
// create and setup camera
130222
OSPCamera camera = ospNewCamera("perspective");
@@ -137,15 +229,18 @@ namespace osprayImpl
137229

138230
// create and setup model and mesh
139231
OSPGeometry mesh = ospNewGeometry("triangles");
140-
OSPData data = ospNewData(4, OSP_FLOAT3A, vertex); // OSP_FLOAT3 format is also supported for vertex positions
232+
OSPData data = ospNewData(vertex.size() / 4, OSP_FLOAT3A, &vertex[0]); // OSP_FLOAT3 format is also supported for vertex positions
233+
//OSPData data = ospNewData(4, OSP_FLOAT3A, vertex_example); // OSP_FLOAT3 format is also supported for vertex positions
141234
ospCommit(data);
142235
ospSetData(mesh, "vertex", data);
143236

144-
data = ospNewData(4, OSP_FLOAT4, color);
237+
data = ospNewData(vertex.size() / 4, OSP_FLOAT4, &color[0]);
238+
//data = ospNewData(4, OSP_FLOAT4, color_example);
145239
ospCommit(data);
146240
ospSetData(mesh, "vertex.color", data);
147241

148-
data = ospNewData(2, OSP_INT3, index); // OSP_INT4 format is also supported for triangle indices
242+
data = ospNewData(index.size() / 3, OSP_INT3, &index[0]); // OSP_INT4 format is also supported for triangle indices
243+
//data = ospNewData(2, OSP_INT3, index_example); // OSP_INT4 format is also supported for triangle indices
149244
ospCommit(data);
150245
ospSetData(mesh, "index", data);
151246

@@ -156,7 +251,6 @@ namespace osprayImpl
156251
ospAddGeometry(world, mesh);
157252
ospCommit(world);
158253

159-
160254
// create renderer
161255
OSPRenderer renderer = ospNewRenderer("scivis"); // choose Scientific Visualization renderer
162256

@@ -184,7 +278,9 @@ namespace osprayImpl
184278

185279
// access framebuffer and write its content as PPM file
186280
const uint32_t * fb = (uint32_t*)ospMapFrameBuffer(framebuffer, OSP_FB_COLOR);
187-
writePPM("firstFrame.ppm", imgSize, fb);
281+
//std::ostringstream fileName;
282+
//fileName << "firstFrame" << cameraSteps << ".ppm";
283+
//writePPM(fileName.str().c_str(), imgSize, fb);
188284
ospUnmapFrameBuffer(fb, framebuffer);
189285

190286

@@ -193,7 +289,9 @@ namespace osprayImpl
193289
ospRenderFrame(framebuffer, renderer, OSP_FB_COLOR | OSP_FB_ACCUM);
194290

195291
fb = (uint32_t*)ospMapFrameBuffer(framebuffer, OSP_FB_COLOR);
196-
writePPM("accumulatedFrame.ppm", imgSize, fb);
292+
std::ostringstream fileNameAcc;
293+
fileNameAcc << "accumulatedFrame" << cameraSteps << ".ppm";
294+
writePPM(fileNameAcc.str().c_str(), imgSize, fb);
197295
ospUnmapFrameBuffer(fb, framebuffer);
198296

199297
return 0;
@@ -205,8 +303,10 @@ TEST_P(OsprayFieldRenderTest, RenderLatVolWithOspray)
205303
{
206304
Log::get() << INFO << "Start ShowField::execute" << std::endl;
207305

208-
209-
osprayImpl::mainFunc();
306+
for (int inc : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
307+
{
308+
osprayImpl::renderLatVol(latVol, inc*0.2);
309+
}
210310

211311
FAIL() << "todo";
212312

@@ -217,7 +317,7 @@ TEST_P(OsprayFieldRenderTest, RenderLatVolWithOspray)
217317
INSTANTIATE_TEST_CASE_P(
218318
RenderLatVolWithOspray,
219319
OsprayFieldRenderTest,
220-
Values(20//, 40, 60, 80
320+
Values(2//, 40, 60, 80
221321
//, 100, 120, 150//, //200 //to speed up make test
222322
//, 256 // probably runs out of memory
223323
)

0 commit comments

Comments
 (0)