Skip to content

Commit

Permalink
Implement decoration generation for data files
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Sep 13, 2024
1 parent a6bd97b commit 17c1052
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ namespace
DataSeriesPattern::forDatatype<DataPointType::BodyForce>("_fx", "_fy", "_fz"),

// extra
DataSeriesPattern::forDatatype<DataPointType::BodyForce>("_x", "_y", "_z"),
DataSeriesPattern::forDatatype<DataPointType::Point>("_x", "_y", "_z"),
};
};

Expand Down Expand Up @@ -272,6 +272,21 @@ namespace

std::vector<DataSeriesAnnotation> m_Annotations;
};

// Returns the elements associated with one datapoint (e.g. [x, y, z])
std::vector<double> extractDataPoint(
const SimTK::State& state,
const OpenSim::Storage& storage,
const DataSeriesAnnotation& annotation)
{
// lol, `OpenSim::Storage` API, etc.
int aN = annotation.firstColumnOffset + static_cast<int>(numElementsIn(annotation.dataType));
std::vector<double> buffer(aN);
double* p = buffer.data();
storage.getDataAtTime(state.getTime(), aN, &p);
buffer.erase(buffer.begin(), buffer.begin() + annotation.firstColumnOffset);
return buffer;
}
}

// ui-independent graphics helpers
Expand Down Expand Up @@ -302,10 +317,21 @@ namespace
template<>
void generateDecorations<DataPointType::BodyForce>(
const SimTK::State&,
std::span<const double, 3>,
SimTK::Array_<SimTK::DecorativeGeometry>&)
std::span<const double, 3> data,
SimTK::Array_<SimTK::DecorativeGeometry>& out)
{
// TODO
const SimTK::Vec3 position = { data[0], data[1], data[2] };
if (not position.isNaN() and position.normSqr() > SimTK::Eps) {
SimTK::DecorativeArrow arrow{
SimTK::Vec3(0.0),
position.normalize(),
};
arrow.setScaleFactors({1, 1, 0.00001});
arrow.setColor(to<SimTK::Vec3>(Color::orange()));
arrow.setLineThickness(0.01);
arrow.setTipLength(0.1);
out.push_back(arrow);
}
}

template<>
Expand Down Expand Up @@ -336,12 +362,24 @@ namespace
}

void generateDecorations(
const SimTK::State&,
const OpenSim::Storage&,
const DataSeriesAnnotation&,
SimTK::Array_<SimTK::DecorativeGeometry>&)
const SimTK::State& state,
const OpenSim::Storage& storage,
const DataSeriesAnnotation& annotation,
SimTK::Array_<SimTK::DecorativeGeometry>& out)
{
// TODO
const auto data = extractDataPoint(state, storage, annotation);
OSC_ASSERT_ALWAYS(data.size() == numElementsIn(annotation.dataType));

static_assert(num_options<DataPointType>() == 5);
switch (annotation.dataType) {
case DataPointType::Point: generateDecorations<DataPointType::Point>( state, std::span<const double, numElementsIn(DataPointType::Point)>{data}, out); break;
case DataPointType::PointForce: generateDecorations<DataPointType::PointForce>( state, std::span<const double, numElementsIn(DataPointType::PointForce)>{data}, out); break;
case DataPointType::BodyForce: generateDecorations<DataPointType::BodyForce>( state, std::span<const double, numElementsIn(DataPointType::BodyForce)>{data}, out); break;
case DataPointType::Orientation: generateDecorations<DataPointType::Orientation>( state, std::span<const double, numElementsIn(DataPointType::Orientation)>{data}, out); break;

case DataPointType::Unknown: break; // do nothing
default: break; // do nothing
}
}
}

Expand Down
59 changes: 20 additions & 39 deletions src/oscar_simbody/SimTKDecorationGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <oscar/Graphics/Color.h>
#include <oscar/Graphics/Scene/SceneCache.h>
#include <oscar/Graphics/Scene/SceneDecoration.h>
#include <oscar/Graphics/Scene/SceneHelpers.h>
#include <oscar/Maths/LineSegment.h>
#include <oscar/Maths/MathHelpers.h>
#include <oscar/Maths/Vec3.h>
Expand Down Expand Up @@ -66,7 +67,7 @@ namespace
}

// creates a geometry-to-ground transform for the given geometry
Transform ToOscTransform(
Transform ToOscTransformWithoutScaling(
const SimTK::SimbodyMatterSubsystem& matter,
const SimTK::State& state,
const SimTK::DecorativeGeometry& g)
Expand All @@ -75,7 +76,7 @@ namespace
const SimTK::Transform& body2ground = mobod.getBodyTransform(state);
const SimTK::Transform& decoration2body = g.getTransform();

return to<Transform>(body2ground * decoration2body).with_scale(GetScaleFactors(g));
return to<Transform>(body2ground * decoration2body);
}

size_t hash_of(const SimTK::Vec3& v)
Expand Down Expand Up @@ -130,9 +131,14 @@ namespace
}

private:
Transform ToOscTransformWithoutScaling(const SimTK::DecorativeGeometry& d) const
{
return ::ToOscTransformWithoutScaling(m_Matter, m_State, d);
}

Transform ToOscTransform(const SimTK::DecorativeGeometry& d) const
{
return ::ToOscTransform(m_Matter, m_State, d);
return ToOscTransformWithoutScaling(d).with_scale(GetScaleFactors(d));
}

void implementPointGeometry(const SimTK::DecorativePoint&) final
Expand Down Expand Up @@ -324,42 +330,17 @@ namespace

void implementArrowGeometry(const SimTK::DecorativeArrow& d) final
{
const Transform t = ToOscTransform(d);

const Vec3 startBase = to<Vec3>(d.getStartPoint());
const Vec3 endBase = to<Vec3>(d.getEndPoint());

const Vec3 start = transform_point(t, startBase);
const Vec3 end = transform_point(t, endBase);

const Vec3 direction = normalize(end - start);

const Vec3 neckStart = start;
const Vec3 neckEnd = end - (m_FixupScaleFactor * static_cast<float>(d.getTipLength()) * direction);
const Vec3 headStart = neckEnd;
const Vec3 headEnd = end;

const float neck_thickness = m_FixupScaleFactor * static_cast<float>(d.getLineThickness());
const float head_thickness = 1.75f * neck_thickness;

const Color color = GetColor(d);
const auto flags = GetFlags(d);

// emit neck cylinder
m_Consumer(SceneDecoration{
.mesh = m_MeshCache.cylinder_mesh(),
.transform = cylinder_to_line_segment_transform({neckStart, neckEnd}, neck_thickness),
.shading = color,
.flags = flags,
});

// emit head cone
m_Consumer({
.mesh = m_MeshCache.cone_mesh(),
.transform = cylinder_to_line_segment_transform({headStart, headEnd}, head_thickness),
.shading = color,
.flags = flags,
});
const Transform t = ToOscTransformWithoutScaling(d);
const ArrowProperties p = {
.start = t * to<Vec3>(d.getStartPoint()),
.end = t * to<Vec3>(d.getEndPoint()),
.tip_length = static_cast<float>(d.getTipLength()),
.neck_thickness = m_FixupScaleFactor * static_cast<float>(d.getLineThickness()),
.head_thickness = 1.75f * m_FixupScaleFactor * static_cast<float>(d.getLineThickness()),
.color = GetColor(d),
.decoration_flags = GetFlags(d),
};
draw_arrow(m_MeshCache, p, m_Consumer);
}

void implementTorusGeometry(const SimTK::DecorativeTorus& d) final
Expand Down

0 comments on commit 17c1052

Please sign in to comment.