Fem: Update constraint transform

This commit is contained in:
marioalexis
2024-05-28 00:37:08 -03:00
parent 25febcd7af
commit d228d50755
16 changed files with 535 additions and 498 deletions

View File

@@ -27,10 +27,12 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <Inventor/SbMatrix.h>
#include <Inventor/SbRotation.h>
#include <Inventor/nodes/SoMaterial.h>
#include <Inventor/SbVec3f.h>
#include <Inventor/nodes/SoSeparator.h>
#include <cmath>
#include <Inventor/nodes/SoSwitch.h>
#include <Inventor/nodes/SoTransform.h>
#endif
#include "Mod/Fem/App/FemConstraintTransform.h"
@@ -46,6 +48,7 @@ PROPERTY_SOURCE(FemGui::ViewProviderFemConstraintTransform, FemGui::ViewProvider
ViewProviderFemConstraintTransform::ViewProviderFemConstraintTransform()
{
sPixmap = "FEM_ConstraintTransform";
loadSymbol((resourceSymbolDir + "ConstraintTransform.iv").c_str());
}
ViewProviderFemConstraintTransform::~ViewProviderFemConstraintTransform() = default;
@@ -91,215 +94,82 @@ bool ViewProviderFemConstraintTransform::setEdit(int ModNum)
}
}
#define HEIGHTAXIS (20)
#define RADIUSAXIS (0.8)
#define ARROWLENGTH (3)
#define ARROWHEADRADIUS (ARROWLENGTH / 3)
#define LENGTHDISC (0.25)
#define RADIUSDISC (0.8)
void ViewProviderFemConstraintTransform::updateData(const App::Property* prop)
{
// Gets called whenever a property of the attached object changes
Fem::ConstraintTransform* pcConstraint =
static_cast<Fem::ConstraintTransform*>(this->getObject());
float scaledradiusaxis =
RADIUSAXIS * pcConstraint->Scale.getValue(); // OvG: Calculate scaled values once only
float scaledheightaxis = HEIGHTAXIS * pcConstraint->Scale.getValue();
float scaledheadradiusA =
ARROWHEADRADIUS * pcConstraint->Scale.getValue(); // OvG: Calculate scaled values once only
float scaledlengthA = ARROWLENGTH * pcConstraint->Scale.getValue();
std::string transform_type = pcConstraint->TransformType.getValueAsString();
if (transform_type == "Rectangular") {
auto obj = static_cast<Fem::ConstraintTransform*>(this->getObject());
if (prop == &pcConstraint->Points) {
const std::vector<Base::Vector3d>& points = pcConstraint->Points.getValues();
const std::vector<Base::Vector3d>& normals = pcConstraint->Normals.getValues();
if (points.size() != normals.size()) {
return;
if (prop == &obj->Rotation) {
updateSymbol();
}
else if (prop == &obj->TransformType || prop == &obj->References) {
std::string transType = obj->TransformType.getValueAsString();
auto sw = static_cast<SoSwitch*>(getSymbolSeparator()->getChild(0));
auto swExtra = static_cast<SoSwitch*>(getExtraSymbolSeparator()->getChild(0));
if (transType == "Rectangular") {
sw->whichChild.setValue(0);
swExtra->whichChild.setValue(-1);
}
else if (transType == "Cylindrical") {
sw->whichChild.setValue(1);
if (obj->References.getSize()) {
swExtra->whichChild.setValue(0);
}
std::vector<Base::Vector3d>::const_iterator n = normals.begin();
// Points and Normals are always updated together
Gui::coinRemoveAllChildren(pShapeSep);
for (const auto& point : points) {
SbVec3f base(point.x, point.y, point.z);
SbVec3f basex(point.x, point.y, point.z);
SbVec3f basey(point.x, point.y, point.z);
double x_axis_x = 1;
double x_axis_y = 0;
double x_axis_z = 0;
double y_axis_x = 0;
double y_axis_y = 1;
double y_axis_z = 0;
double z_axis_x = 0;
double z_axis_y = 0;
double z_axis_z = 1;
double rot_x = (pcConstraint->X_rot.getValue() * (M_PI / 180));
double rot_y = (pcConstraint->Y_rot.getValue() * (M_PI / 180));
double rot_z = (pcConstraint->Z_rot.getValue() * (M_PI / 180));
double x_axis_x_p;
double x_axis_y_p;
double x_axis_z_p;
double y_axis_x_p;
double y_axis_y_p;
double y_axis_z_p;
double z_axis_x_p;
double z_axis_y_p;
double z_axis_z_p;
if (rot_x != 0) {
x_axis_z_p = x_axis_z * cos(rot_x) - x_axis_y * sin(rot_x);
x_axis_y_p = x_axis_y * cos(rot_x) + x_axis_z * sin(rot_x);
x_axis_z = x_axis_z_p;
x_axis_y = x_axis_y_p;
y_axis_z_p = y_axis_z * cos(rot_x) - y_axis_y * sin(rot_x);
y_axis_y_p = y_axis_y * cos(rot_x) + y_axis_z * sin(rot_x);
y_axis_z = y_axis_z_p;
y_axis_y = y_axis_y_p;
z_axis_z_p = z_axis_z * cos(rot_x) - z_axis_y * sin(rot_x);
z_axis_y_p = z_axis_y * cos(rot_x) + z_axis_z * sin(rot_x);
z_axis_z = z_axis_z_p;
z_axis_y = z_axis_y_p;
}
if (rot_y != 0) {
x_axis_z_p = x_axis_z * cos(rot_y) + x_axis_x * sin(rot_y);
x_axis_x_p = x_axis_x * cos(rot_y) - x_axis_z * sin(rot_y);
x_axis_z = x_axis_z_p;
x_axis_x = x_axis_x_p;
y_axis_z_p = y_axis_z * cos(rot_y) + y_axis_x * sin(rot_y);
y_axis_x_p = y_axis_x * cos(rot_y) - y_axis_z * sin(rot_y);
y_axis_z = y_axis_z_p;
y_axis_x = y_axis_x_p;
z_axis_z_p = z_axis_z * cos(rot_y) + z_axis_x * sin(rot_y);
z_axis_x_p = z_axis_x * cos(rot_y) - z_axis_z * sin(rot_y);
z_axis_z = z_axis_z_p;
z_axis_x = z_axis_x_p;
}
if (rot_z != 0) {
x_axis_x_p = x_axis_x * cos(rot_z) + x_axis_y * sin(rot_z);
x_axis_y_p = x_axis_y * cos(rot_z) - x_axis_x * sin(rot_z);
x_axis_x = x_axis_x_p;
x_axis_y = x_axis_y_p;
y_axis_x_p = y_axis_x * cos(rot_z) + y_axis_y * sin(rot_z);
y_axis_y_p = y_axis_y * cos(rot_z) - y_axis_x * sin(rot_z);
y_axis_x = y_axis_x_p;
y_axis_y = y_axis_y_p;
z_axis_x_p = z_axis_x * cos(rot_z) + z_axis_y * sin(rot_z);
z_axis_y_p = z_axis_y * cos(rot_z) - z_axis_x * sin(rot_z);
z_axis_x = z_axis_x_p;
z_axis_y = z_axis_y_p;
}
SbVec3f dirz(z_axis_x, z_axis_y, z_axis_z);
SbRotation rot(SbVec3f(0, 1, 0), dirz);
SbVec3f dirx(x_axis_x, x_axis_y, x_axis_z);
SbRotation rotx(SbVec3f(0, 1, 0), dirx);
SbVec3f diry(y_axis_x, y_axis_y, y_axis_z);
SbRotation roty(SbVec3f(0, 1, 0), diry);
base = base + dirz * scaledlengthA * 0.75f;
basex = basex + dirx * scaledlengthA * 0.65f;
basey = basey + diry * scaledlengthA * 0.65f;
SoSeparator* sep = new SoSeparator();
SoMaterial* myMaterial = new SoMaterial;
myMaterial->diffuseColor.set1Value(0, SbColor(0, 0, 1)); // RGB
sep->addChild(myMaterial);
createPlacement(sep, base, rot);
createArrow(sep, scaledlengthA * 0.75, scaledheadradiusA * 0.9); // OvG: Scaling
pShapeSep->addChild(sep);
SoSeparator* sepx = new SoSeparator();
SoMaterial* myMaterialx = new SoMaterial;
myMaterialx->diffuseColor.set1Value(0, SbColor(1, 0, 0)); // RGB
sepx->addChild(myMaterialx);
createPlacement(sepx, basex, rotx);
createArrow(sepx, scaledlengthA * 0.65, scaledheadradiusA * 0.65); // OvG: Scaling
pShapeSep->addChild(sepx);
SoSeparator* sepy = new SoSeparator();
SoMaterial* myMaterialy = new SoMaterial;
myMaterialy->diffuseColor.set1Value(0, SbColor(0, 1, 0)); // RGB
sepy->addChild(myMaterialy);
createPlacement(sepy, basey, roty);
createArrow(sepy, scaledlengthA * 0.65, scaledheadradiusA * 0.65); // OvG: Scaling
pShapeSep->addChild(sepy);
n++;
else {
swExtra->whichChild.setValue(-1);
}
}
updateSymbol();
}
else if (transform_type == "Cylindrical") {
// Points and Normals are always updated together
Gui::coinRemoveAllChildren(pShapeSep);
const std::vector<Base::Vector3d>& points = pcConstraint->Points.getValues();
const std::vector<Base::Vector3d>& normals = pcConstraint->Normals.getValues();
if (points.size() != normals.size()) {
return;
}
std::vector<Base::Vector3d>::const_iterator n = normals.begin();
if (!points.empty()) {
Base::Vector3d base = pcConstraint->BasePoint.getValue();
Base::Vector3d axis = pcConstraint->Axis.getValue();
SbVec3f b(base.x, base.y, base.z);
SbVec3f ax(axis.x, axis.y, axis.z);
SbRotation rots(SbVec3f(0, -1, 0), ax);
b = b - ax * scaledheightaxis / 2;
SoSeparator* sepAx = new SoSeparator();
SoMaterial* myMaterial = new SoMaterial;
myMaterial->diffuseColor.set1Value(0, SbColor(0, 0, 1)); // RGB
sepAx->addChild(myMaterial);
createPlacement(sepAx, b, rots);
createArrow(sepAx, scaledheightaxis, scaledradiusaxis);
pShapeSep->addChild(sepAx);
}
for (const auto& point : points) {
SbVec3f base(point.x, point.y, point.z);
SbVec3f dir(n->x, n->y, n->z);
base = base + dir * scaledlengthA; // OvG: Scaling
SbRotation rot(SbVec3f(0, 1, 0), dir);
SoSeparator* sep = new SoSeparator();
SoMaterial* myMaterials = new SoMaterial;
myMaterials->diffuseColor.set1Value(0, SbColor(1, 0, 0)); // RGB
sep->addChild(myMaterials);
createPlacement(sep, base, rot);
createArrow(sep, scaledlengthA, scaledheadradiusA); // OvG: Scaling
pShapeSep->addChild(sep);
n++;
}
else if (prop == &obj->BasePoint || prop == &obj->Axis) {
updateSymbol();
}
// Gets called whenever a property of the attached object changes
ViewProviderFemConstraint::updateData(prop);
}
void ViewProviderFemConstraintTransform::transformSymbol(const Base::Vector3d& point,
const Base::Vector3d& normal,
SbMatrix& mat) const
{
auto obj = static_cast<const Fem::ConstraintTransform*>(this->getObject());
std::string transType = obj->TransformType.getValueAsString();
if (transType == "Rectangular") {
Base::Rotation rot = obj->Rotation.getValue();
Base::Vector3d axis;
double angle;
rot.getValue(axis, angle);
float s = obj->getScaleFactor();
mat.setTransform(SbVec3f(point.x, point.y, point.z),
SbRotation(SbVec3f(axis.x, axis.y, axis.z), angle),
SbVec3f(s, s, s));
}
else if (transType == "Cylindrical") {
float s = obj->getScaleFactor();
mat.setTransform(SbVec3f(point.x, point.y, point.z),
SbRotation(SbVec3f(0, 1, 0), SbVec3f(normal.x, normal.y, normal.z)),
SbVec3f(s, s, s));
}
}
void ViewProviderFemConstraintTransform::transformExtraSymbol() const
{
auto obj = static_cast<const Fem::ConstraintTransform*>(this->getObject());
std::string transType = obj->TransformType.getValueAsString();
if (transType == "Cylindrical") {
SoTransform* trans = getExtraSymbolTransform();
Base::Vector3d point = obj->BasePoint.getValue();
Base::Vector3d axis = obj->Axis.getValue();
float s = obj->getScaleFactor();
SbMatrix mat;
mat.setTransform(SbVec3f(point.x, point.y, point.z),
SbRotation(SbVec3f(0, 1, 0), SbVec3f(axis.x, axis.y, axis.z)),
SbVec3f(s, s, s));
trans->setMatrix(mat);
}
}