Skip to content

Commit 71cb8a4

Browse files
fredroyhugtalbot
andauthored
[Visual] VisualModelImpl: add data to optionally generate uv coords (#6137)
visualmodelimpl: add data to optionally compute uv coords (instead of being forcefully computed in all cases) Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent b4a3c87 commit 71cb8a4

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

Sofa/Component/Visual/src/sofa/component/visual/VisualModelImpl.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ VisualModelImpl::VisualModelImpl() //const std::string &name, std::string filena
118118
, d_updateNormals (initData (&d_updateNormals, true, "updateNormals", "True if normals should be updated at each iteration"))
119119
, d_computeTangents (initData (&d_computeTangents, false, "computeTangents", "True if tangents should be computed at startup"))
120120
, d_updateTangents (initData (&d_updateTangents, true, "updateTangents", "True if tangents should be updated at each iteration"))
121+
, d_computeTextureCoordinates(initData (&d_computeTextureCoordinates, false, "computeTextureCoordinates", "True if texture coordinates should be computed at startup, using UV sphere projection"))
121122
, d_handleDynamicTopology (initData (&d_handleDynamicTopology, true, "handleDynamicTopology", "True if topological changes should be handled"))
122123
, d_fixMergedUVSeams (initData (&d_fixMergedUVSeams, true, "fixMergedUVSeams", "True if UV seams should be handled even when duplicate UVs are merged"))
123124
, d_keepLines (initData (&d_keepLines, false, "keepLines", "keep and draw lines (false by default)"))
@@ -1176,7 +1177,8 @@ void VisualModelImpl::computeUVSphereProjection()
11761177
const sofa::core::visual::VisualParams* vparams = sofa::core::visual::VisualParams::defaultInstance();
11771178
this->computeBBox(vparams);
11781179

1179-
auto center = (this->f_bbox.getValue().minBBox() + this->f_bbox.getValue().maxBBox())*0.5f;
1180+
const auto bbox = this->f_bbox.getValue();
1181+
const auto center = (bbox.minBBox() + bbox.maxBBox())*0.5f;
11801182

11811183
// Map mesh vertices to sphere
11821184
// transform cart to spherical coordinates (r, theta, phi) and sphere to cart back with radius = 1
@@ -1185,29 +1187,25 @@ void VisualModelImpl::computeUVSphereProjection()
11851187
const std::size_t nbrV = coords.size();
11861188
VecCoord m_sphereV;
11871189
m_sphereV.resize(nbrV);
1188-
1189-
VecTexCoord& vtexcoords = *(d_vtexcoords.beginEdit());
1190+
1191+
auto vtexcoords = sofa::helper::getWriteOnlyAccessor(d_vtexcoords);
11901192
vtexcoords.resize(nbrV);
11911193

11921194
for (std::size_t i = 0; i < nbrV; ++i)
11931195
{
1194-
Coord Vcentered = coords[i] - center;
1195-
SReal r = sqrt(Vcentered[0] * Vcentered[0] + Vcentered[1] * Vcentered[1] + Vcentered[2] * Vcentered[2]);
1196-
const SReal theta = acos(Vcentered[2] / r);
1197-
const SReal phi = atan2(Vcentered[1], Vcentered[0]);
1198-
1199-
r = 1.0;
1200-
m_sphereV[i][0] = r * sin(theta)*cos(phi) + center[0];
1201-
m_sphereV[i][1] = r * sin(theta)*sin(phi) + center[1];
1202-
m_sphereV[i][2] = r * cos(theta) + center[2];
1203-
1204-
Coord pos = m_sphereV[i] - center;
1205-
pos.normalize();
1206-
vtexcoords[i][0] = float(0.5 + atan2(pos[1], pos[0]) / (2 * R_PI));
1207-
vtexcoords[i][1] = float(0.5 - asin(pos[2]) / R_PI);
1196+
const Coord Vcentered = coords[i] - center;
1197+
const auto r = sqrt(Vcentered[0] * Vcentered[0] + Vcentered[1] * Vcentered[1] + Vcentered[2] * Vcentered[2]);
1198+
const auto theta = acos(Vcentered[2] / r);
1199+
const auto phi = atan2(Vcentered[1], Vcentered[0]);
1200+
1201+
m_sphereV[i][0] = sin(theta)*cos(phi) + center[0];
1202+
m_sphereV[i][1] = sin(theta)*sin(phi) + center[1];
1203+
m_sphereV[i][2] = cos(theta) + center[2];
1204+
1205+
const Coord pos = (m_sphereV[i] - center).normalized();
1206+
vtexcoords[i][0] = static_cast<TexCoord::value_type>(0.5 + atan2(pos[1], pos[0]) / (2 * R_PI));
1207+
vtexcoords[i][1] = static_cast<TexCoord::value_type>(0.5 - asin(pos[2]) / R_PI);
12081208
}
1209-
1210-
d_vtexcoords.endEdit();
12111209
}
12121210

12131211
void VisualModelImpl::flipFaces()
@@ -1322,7 +1320,7 @@ void VisualModelImpl::doUpdateVisual(const core::visual::VisualParams* vparams)
13221320
SCOPED_TIMER_VARNAME(t, "VisualModelImpl::computeTangents");
13231321
computeTangents();
13241322
}
1325-
if (d_vtexcoords.getValue().size() == 0)
1323+
if(d_computeTextureCoordinates.getValue())
13261324
{
13271325
SCOPED_TIMER_VARNAME(t, "VisualModelImpl::computeUVSphereProjection");
13281326
computeUVSphereProjection();

Sofa/Component/Visual/src/sofa/component/visual/VisualModelImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class SOFA_COMPONENT_VISUAL_API VisualModelImpl : public core::visual::VisualMod
8181
Data<bool> d_updateNormals; ///< True if normals should be updated at each iteration
8282
Data<bool> d_computeTangents; ///< True if tangents should be computed at startup
8383
Data<bool> d_updateTangents; ///< True if tangents should be updated at each iteration
84+
Data<bool> d_computeTextureCoordinates; ///< True if texture coordinates should be computed at startup, using UV sphere projection
8485
Data<bool> d_handleDynamicTopology; ///< True if topological changes should be handled
8586
Data<bool> d_fixMergedUVSeams; ///< True if UV seams should be handled even when duplicate UVs are merged
8687
Data<bool> d_keepLines; ///< keep and draw lines (false by default)

0 commit comments

Comments
 (0)