Skip to content

Commit 7fe99bb

Browse files
committed
Fix viewer for 2D fixtures
1 parent fadb532 commit 7fe99bb

File tree

3 files changed

+81
-34
lines changed

3 files changed

+81
-34
lines changed

src/viewer/UISimState.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,30 @@ void UISimState::load_scene()
6363
m_state.problem_ptr->codim_vertices_to_vertices());
6464
m_mesh_data.update_velocities(v);
6565

66-
Eigen::VectorXi vertex_types;
66+
Eigen::VectorXi vertex_types, vertex_ids;
6767
if (m_state.problem_ptr->is_rb_problem()) {
6868
const auto& bodies =
6969
std::dynamic_pointer_cast<RigidBodyProblem>(m_state.problem_ptr)
7070
->m_assembler;
7171
vertex_types.resize(bodies.num_vertices());
72+
vertex_ids.resize(bodies.num_vertices());
7273
int start_i = 0;
74+
int i = 0;
7375
for (const auto& body : bodies.m_rbs) {
7476
vertex_types.segment(start_i, body.vertices.rows())
7577
.setConstant(int(body.type));
78+
vertex_ids.segment(start_i, body.vertices.rows()).setConstant(i++);
7679
start_i += body.vertices.rows();
7780
}
81+
7882
} else {
7983
vertex_types =
8084
m_state.problem_ptr->vertex_dof_fixed().rowwise().all().cast<int>();
8185
vertex_types *= 2; // 0 is static, 1 is kinematic, 2 is dynamic
86+
vertex_ids = m_state.problem_ptr->group_ids();
8287
}
8388
m_mesh_data.set_vertex_types(vertex_types);
89+
m_mesh_data.set_vertex_ids(vertex_ids);
8490

8591
if (m_state.problem_ptr->is_rb_problem()) {
8692
m_com_data.set_coms(
@@ -111,6 +117,11 @@ void UISimState::load_scene()
111117
// q, dim == 2.0 ? mesh_data->mE : mesh_data->mF);
112118

113119
// Default colors
120+
// if (q.cols() == 2) {
121+
ps::view::bgColor = { { 0.0f, 0.0f, 0.0f, 0.0f } }; // Black for 2D
122+
// } else {
123+
// ps::view::bgColor = { { 1.0f, 1.0f, 1.0f, 0.0f } }; // White for 3D
124+
// }
114125
// ps::view::bgColor = { { 0.3f, 0.3f, 0.5f, 0.0f } };
115126

116127
// Camera parameters

src/viewer/viewer_data.cpp

Lines changed: 68 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@ namespace ipc::rigid {
1616
/// MeshData
1717
// =============================================================================
1818

19-
const glm::vec3 MeshData::EDGE_COLOR(0.0, 0.0, 0.0); // #000000
19+
const glm::vec3 MeshData::EDGE_COLOR(1.0, 1.0, 1.0); // #FFFFFF
2020
const glm::vec3
2121
MeshData::STATIC_COLOR(0xB3 / 255.0, 0xB3 / 255.0, 0xB3 / 255.0); // #B3B3B3
2222
// const glm::vec3 MeshData::KINEMATIC_COLOR(1.0, 0.5, 0.0); // #FF8000
2323
const glm::vec3 MeshData::KINEMATIC_COLOR(
2424
0xE3 / 255.0, 0x82 / 255.0, 0x1C / 255.0); // #E3821C
2525

2626
void MeshData::set_mesh(
27-
const Eigen::MatrixXd& V,
27+
const Eigen::MatrixXd& _V,
2828
const Eigen::MatrixXi& E,
2929
const Eigen::MatrixXi& F,
3030
const std::vector<int>& CE2E,
3131
const std::vector<int>& CV2V)
3232
{
33-
ps_surface_mesh = ps::registerSurfaceMesh("mesh", V, F);
33+
const int dim = _V.cols();
34+
Eigen::MatrixXd V(_V.rows(), 3);
35+
V.leftCols(dim) = _V;
36+
if (dim == 2) {
37+
V.col(2).setZero();
38+
}
3439

35-
ps_velocities = ps_surface_mesh->addVertexVectorQuantity(
36-
"velocities", Eigen::MatrixXd::Zero(V.rows(), V.cols()));
37-
ps_velocities->setEnabled(false);
38-
ps_velocities->setVectorColor(velocity_color);
40+
if (F.size() > 0) {
41+
ps_surface_mesh = ps::registerSurfaceMesh("mesh", V, F);
42+
ps_velocities = ps_surface_mesh->addVertexVectorQuantity(
43+
"velocities", Eigen::MatrixXd::Zero(V.rows(), V.cols()));
44+
ps_velocities->setEnabled(false);
45+
ps_velocities->setVectorColor(velocity_color);
46+
}
3947

4048
if (CE2E.size() > 0) {
4149
Eigen::MatrixXi CE;
@@ -51,9 +59,14 @@ void MeshData::set_mesh(
5159

5260
if (CE.rows() != 0) {
5361
ps_codim_edges = ps::registerCurveNetwork("edges", CE_V, CE_E);
54-
ps_codim_edges->setRadius(5e-4);
55-
ps_codim_edges->setColor(EDGE_COLOR);
62+
ps_codim_edges->setRadius(dim == 2 ? 1e-3 : 5e-4);
63+
if (dim == 3) {
64+
ps_codim_edges->setColor(EDGE_COLOR);
65+
}
5666
}
67+
} else if (ps_codim_edges != nullptr) {
68+
ps::removeStructure(ps_codim_edges);
69+
ps_codim_edges = nullptr;
5770
}
5871

5972
if (CV2V.size() > 0) {
@@ -65,11 +78,21 @@ void MeshData::set_mesh(
6578
ps_codim_points = ps::registerPointCloud("vertices", CV);
6679
ps_codim_points->setPointRadius(5e-4);
6780
ps_codim_points->setPointColor(EDGE_COLOR);
81+
} else if (ps_codim_points != nullptr) {
82+
ps::removeStructure(ps_codim_points);
83+
ps_codim_points = nullptr;
6884
}
6985
}
7086

71-
void MeshData::update_vertices(const Eigen::MatrixXd& V)
87+
void MeshData::update_vertices(const Eigen::MatrixXd& _V)
7288
{
89+
const int dim = _V.cols();
90+
Eigen::MatrixXd V(_V.rows(), 3);
91+
V.leftCols(dim) = _V;
92+
if (dim == 2) {
93+
V.col(2).setZero();
94+
}
95+
7396
if (ps_surface_mesh != nullptr) {
7497
ps_surface_mesh->updateVertexPositions(V);
7598
}
@@ -95,19 +118,33 @@ void MeshData::update_velocities(const Eigen::MatrixXd& velocities)
95118

96119
void MeshData::set_vertex_types(const Eigen::VectorXi& vertex_types)
97120
{
98-
if (ps_surface_mesh != nullptr) {
99-
std::vector<glm::vec3> colors(vertex_types.size());
100-
for (int i = 0; i < vertex_types.size(); i++) {
101-
if (vertex_types(i) == 0) {
102-
colors[i] = STATIC_COLOR;
103-
} else if (vertex_types(i) == 1) {
104-
colors[i] = KINEMATIC_COLOR;
105-
} else {
106-
colors[i] = mesh_color; // Default color for other types
107-
}
121+
std::vector<glm::vec3> colors(vertex_types.size());
122+
for (int i = 0; i < vertex_types.size(); i++) {
123+
if (vertex_types(i) == 0) {
124+
colors[i] = STATIC_COLOR;
125+
} else if (vertex_types(i) == 1) {
126+
colors[i] = KINEMATIC_COLOR;
127+
} else {
128+
colors[i] = mesh_color; // Default color for other types
108129
}
109-
ps_surface_mesh->addVertexColorQuantity("type", colors)
110-
->setEnabled(true);
130+
}
131+
if (ps_surface_mesh != nullptr) {
132+
ps_surface_mesh->addVertexColorQuantity("type", colors);
133+
} else if (ps_codim_edges != nullptr) {
134+
ps_codim_edges->addNodeColorQuantity("type", colors);
135+
}
136+
}
137+
138+
void MeshData::set_vertex_ids(const Eigen::VectorXi& ids)
139+
{
140+
if (ps_surface_mesh != nullptr) {
141+
auto tmp = ps_surface_mesh->addVertexScalarQuantity("id", ids);
142+
tmp->setEnabled(true);
143+
tmp->setColorMap("rainbow");
144+
} else if (ps_codim_edges != nullptr) {
145+
auto tmp = ps_codim_edges->addNodeScalarQuantity("id", ids);
146+
tmp->setEnabled(true);
147+
tmp->setColorMap("rainbow");
111148
}
112149
}
113150

@@ -118,17 +155,16 @@ void MeshData::set_vertex_types(const Eigen::VectorXi& vertex_types)
118155
void CoMData::set_coms(const ipc::rigid::PosesD& poses)
119156
{
120157
int dim = poses.size() ? poses[0].dim() : 0;
121-
Eigen::MatrixXd coms(poses.size(), dim);
122-
std::vector<Eigen::MatrixXd> principle_axes(
123-
dim, Eigen::MatrixXd(poses.size(), dim));
158+
Eigen::MatrixXd coms = Eigen::MatrixXd::Zero(poses.size(), 3);
159+
std::vector<Eigen::MatrixXd> principle_axes(dim, coms);
124160

125161
for (int i = 0; i < poses.size(); i++) {
126-
coms.row(i) = poses[i].position;
162+
coms.row(i).head(dim) = poses[i].position;
127163
MatrixMax3d R = poses[i].construct_rotation_matrix();
128164
for (int j = 0; j < dim; j++) {
129165
VectorMax3d axis = VectorMax3d::Zero(dim);
130166
axis(j) = 1;
131-
principle_axes[j].row(i) = R * axis;
167+
principle_axes[j].row(i).head(dim) = R * axis;
132168
}
133169
}
134170

@@ -150,21 +186,20 @@ void CoMData::set_coms(const ipc::rigid::PosesD& poses)
150186
void CoMData::update_coms(const ipc::rigid::PosesD& poses)
151187
{
152188
int dim = poses.size() ? poses[0].dim() : 0;
153-
Eigen::MatrixXd coms(poses.size(), dim);
154-
std::vector<Eigen::MatrixXd> principle_axes(
155-
dim, Eigen::MatrixXd(poses.size(), dim));
189+
Eigen::MatrixXd coms = Eigen::MatrixXd::Zero(poses.size(), 3);
190+
std::vector<Eigen::MatrixXd> principle_axes(dim, coms);
156191

157192
for (int i = 0; i < poses.size(); i++) {
158-
coms.row(i) = poses[i].position;
193+
coms.row(i).head(dim) = poses[i].position;
159194
MatrixMax3d R = poses[i].construct_rotation_matrix();
160195
for (int j = 0; j < dim; j++) {
161196
VectorMax3d axis = VectorMax3d::Zero(dim);
162197
axis(j) = 1;
163-
principle_axes[j].row(i) = R * axis;
198+
principle_axes[j].row(i).head(dim) = R * axis;
164199
}
165200
}
166201

167-
ps_coms->updatePointPositions(coms);
202+
ps_coms = ps::registerPointCloud("CoMs", coms);
168203

169204
for (int j = 0; j < dim; j++) {
170205
ps_coms->addVectorQuantity(

src/viewer/viewer_data.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class MeshData {
3030
void update_vertices(const Eigen::MatrixXd& V);
3131
void update_velocities(const Eigen::MatrixXd& velocities);
3232
void set_vertex_types(const Eigen::VectorXi& vertex_types);
33+
void set_vertex_ids(const Eigen::VectorXi& ids);
3334

3435
glm::vec3 mesh_color = glm::vec3(
3536
0xE3 / 255.0, 0x1C / 255.0, 0x1C / 255.0); // #E31C1C - ALIZARIN

0 commit comments

Comments
 (0)