Skip to content

Commit 8df0169

Browse files
committed
Adding @jessdtate's up vector computation
1 parent a1265f2 commit 8df0169

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

src/Modules/Visualization/InterfaceWithOspray.cc

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ using namespace Dataflow::Networks;
4747
using namespace Modules::Visualization;
4848
using namespace Core;
4949
using namespace Core::Algorithms;
50+
using namespace Core::Geometry;
5051
using namespace Visualization;
5152
using namespace Datatypes;
5253

@@ -151,6 +152,11 @@ namespace detail
151152
return static_cast<float>(state_->getValue(name).toDouble());
152153
}
153154

155+
std::array<float,3> toArray(const Vector& v) const
156+
{
157+
return { static_cast<float>(v.x()), static_cast<float>(v.y()), static_cast<float>(v.z()) };
158+
}
159+
154160
struct FieldData
155161
{
156162
std::vector<float> vertex, color;
@@ -196,23 +202,47 @@ namespace detail
196202
imageBox_.extend(bbox);
197203
auto center = imageBox_.center();
198204
float position[] = { toFloat(Parameters::CameraPositionX), toFloat(Parameters::CameraPositionY), toFloat(Parameters::CameraPositionZ) };
205+
float cam_up[] = { toFloat(Parameters::CameraUpX), toFloat(Parameters::CameraUpY), toFloat(Parameters::CameraUpZ) };
199206
float newDir[] = { static_cast<float>(center.x()) - position[0],
200207
static_cast<float>(center.y()) - position[1],
201208
static_cast<float>(center.z()) - position[2]};
202-
//std::cout << "newDir " << newDir[0] << ", " << newDir[1] << ", " << newDir[2] << std::endl;
209+
203210
state_->setValue(Parameters::CameraViewX, center.x());
204211
state_->setValue(Parameters::CameraViewY, center.y());
205212
state_->setValue(Parameters::CameraViewZ, center.z());
206213
ospSet3fv(camera_, "dir", newDir);
207-
float newUp[] = { newDir[0] / newDir[2], -(newDir[0]*newDir[0] + newDir[2]*newDir[2])/(newDir[1]*newDir[2]) , 1.0f };
208-
state_->setValue(Parameters::CameraUpX, newUp[0]);
209-
state_->setValue(Parameters::CameraUpY, newUp[1]);
210-
state_->setValue(Parameters::CameraUpZ, newUp[2]);
211-
ospSet3fv(camera_, "up", newUp);
214+
auto newUp = getCameraUp(newDir, cam_up);
215+
state_->setValue(Parameters::CameraUpX, newUp.x());
216+
state_->setValue(Parameters::CameraUpY, newUp.y());
217+
state_->setValue(Parameters::CameraUpZ, newUp.z());
218+
ospSet3fv(camera_, "up", toArray(newUp).begin());
212219
ospCommit(camera_);
213220
}
214221
}
215222

223+
Vector getCameraUp(float* newDir, float* cam_up)
224+
{
225+
Vector side(newDir[1]*cam_up[2] - newDir[2]*cam_up[1],
226+
newDir[2]*cam_up[0] - newDir[0]*cam_up[2],
227+
newDir[0]*cam_up[1] - newDir[1]*cam_up[0]);
228+
auto norm_side = side.length();
229+
if (norm_side <= 1e-3)
230+
{
231+
side = Vector(newDir[1], -newDir[0], 0.0);
232+
norm_side = side.length();
233+
if (norm_side <= 1e-3)
234+
{
235+
side = Vector(-newDir[2], 0.0, newDir[0]);
236+
norm_side = side.length();
237+
}
238+
}
239+
side /= norm_side;
240+
241+
return Vector(side[1]*newDir[2] - side[2]*newDir[1],
242+
side[2]*newDir[0] - side[0]*newDir[2],
243+
side[0]*newDir[1] - side[1]*newDir[0]);
244+
}
245+
216246
void fillDataBuffers(FieldHandle field, ColorMapHandle colorMap)
217247
{
218248
auto facade(field->mesh()->getFacade());
@@ -459,7 +489,7 @@ void InterfaceWithOspray::execute()
459489
ospray.addField(field, color);
460490
}
461491
}
462-
492+
463493
for (auto& streamline : streamlines)
464494
{
465495
FieldInformation info(streamline);

0 commit comments

Comments
 (0)