Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Glyphs Normals #2320

Merged
merged 17 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,288 changes: 2,288 additions & 0 deletions src/ExampleNets/regression/Renderer/negative_tensors_normals_deubg.srn5

Large diffs are not rendered by default.

346 changes: 193 additions & 153 deletions src/Graphics/Glyphs/GlyphConstructor.cc

Large diffs are not rendered by default.

46 changes: 27 additions & 19 deletions src/Graphics/Glyphs/GlyphConstructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,51 @@

namespace SCIRun {
namespace Graphics {
struct SCISHARE GlyphData
{
std::vector<Core::Geometry::Vector> points_;
std::vector<Core::Geometry::Vector> normals_;
std::vector<Core::Datatypes::ColorRGB> colors_;
std::vector<size_t> indices_;
size_t numVBOElements_ = 0;
size_t lineIndex_ = 0;
uint32_t offset_ = 0;
};

class SCISHARE GlyphConstructor
{
public:
GlyphConstructor();
void buildObject(Graphics::Datatypes::GeometryObjectSpire& geom, const std::string& uniqueNodeID,
const bool isTransparent, const double transparencyValue,
const Graphics::Datatypes::ColorScheme& colorScheme, RenderState state,
const Graphics::Datatypes::SpireIBO::PRIMITIVE& primIn,
const Core::Geometry::BBox& bbox,
const bool isClippable = true,
const Core::Datatypes::ColorMapHandle colorMap = nullptr);

uint32_t setOffset();
bool normalsValid() const;
void addVertex(const Core::Geometry::Vector& point, const Core::Geometry::Vector& normal,
uint32_t setOffset(Datatypes::SpireIBO::PRIMITIVE prim);
bool normalsValid(Datatypes::SpireIBO::PRIMITIVE prim) const;
void addVertex(Datatypes::SpireIBO::PRIMITIVE prim, const Core::Geometry::Vector& point, const Core::Geometry::Vector& normal,
const Core::Datatypes::ColorRGB& color);
void addVertex(const Core::Geometry::Vector& point, const Core::Datatypes::ColorRGB& color);
void addVertex(Datatypes::SpireIBO::PRIMITIVE prim, const Core::Geometry::Vector& point, const Core::Datatypes::ColorRGB& color);
void addLine(const Core::Geometry::Vector& point1, const Core::Geometry::Vector& point2,
const Core::Datatypes::ColorRGB& color1, const Core::Datatypes::ColorRGB& color2);
void addPoint(const Core::Geometry::Vector& point, const Core::Datatypes::ColorRGB& color);
void addLineIndex();
void addIndex(size_t i);
void addIndices(size_t i1, size_t i2, size_t i3);
void addIndicesToOffset(size_t i1, size_t i2, size_t i3);
void addIndexToOffset(size_t i);
size_t getCurrentIndex() const;
void popIndicesNTimes(int n);
void addLineIndex(Datatypes::SpireIBO::PRIMITIVE prim);
void addIndex(Datatypes::SpireIBO::PRIMITIVE prim, size_t i);
void addIndices(Datatypes::SpireIBO::PRIMITIVE prim, size_t i1, size_t i2, size_t i3);
void addIndicesToOffset(Datatypes::SpireIBO::PRIMITIVE prim, size_t i1, size_t i2, size_t i3);
void addIndexToOffset(Datatypes::SpireIBO::PRIMITIVE prim, size_t i);
size_t getCurrentIndex(Datatypes::SpireIBO::PRIMITIVE prim) const;
void popIndicesNTimes(Datatypes::SpireIBO::PRIMITIVE prim, int n);

private:
const GlyphData& getDataConst(Datatypes::SpireIBO::PRIMITIVE prim) const;
GlyphData& getData(Datatypes::SpireIBO::PRIMITIVE prim);
GlyphData pointData_;
GlyphData lineData_;
GlyphData meshData_;
std::vector<SinCosTable> tables_;
std::vector<Core::Geometry::Vector> points_;
std::vector<Core::Geometry::Vector> normals_;
std::vector<Core::Datatypes::ColorRGB> colors_;
std::vector<size_t> indices_;
size_t numVBOElements_ = 0;
size_t lineIndex_ = 0;
uint32_t offset_ = 0;
};
}}

Expand Down
117 changes: 76 additions & 41 deletions src/Graphics/Glyphs/GlyphGeom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,60 @@ GlyphGeom::GlyphGeom()
}
void GlyphGeom::buildObject(GeometryObjectSpire& geom, const std::string& uniqueNodeID,
const bool isTransparent, const double transparencyValue,
const ColorScheme& colorScheme, RenderState state,
const SpireIBO::PRIMITIVE& primIn, const BBox& bbox,
const ColorScheme& colorScheme, RenderState state, const BBox& bbox,
const bool isClippable, const Core::Datatypes::ColorMapHandle colorMap)
{
constructor_.buildObject(geom, uniqueNodeID, isTransparent, transparencyValue, colorScheme, state,
primIn, bbox, isClippable, colorMap);
bbox, isClippable, colorMap);
}

void GlyphGeom::addArrow(const Point& p1, const Point& p2, double radius, double ratio, int resolution,
const ColorRGB& color1, const ColorRGB& color2, bool render_cylinder_base, bool render_cone_base)
const ColorRGB& color1, const ColorRGB& color2, bool render_cylinder_base,
bool render_cone_base, bool showNormals, double showNormalsScale)
{

VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setColor2(color2);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateArrow(constructor_, radius, ratio, render_cylinder_base, render_cone_base);
}

void GlyphGeom::addSphere(const Point& p, double radius, int resolution, const ColorRGB& color)
void GlyphGeom::addSphere(const Point& p, double radius, int resolution, const ColorRGB& color,
bool showNormals, double showNormalsScale)
{
generateSphere(p, radius, resolution, color);
generateSphere(p, radius, resolution, color, showNormals, showNormalsScale);
}

void GlyphGeom::addComet(const Point& p1, const Point& p2, double radius, int resolution,
const ColorRGB& color1, const ColorRGB& color2, double sphere_extrusion)
const ColorRGB& color1, const ColorRGB& color2, double sphere_extrusion,
bool showNormals, double showNormalsScale)
{
VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setColor2(color2);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateComet(constructor_, radius, sphere_extrusion);
}

void GlyphGeom::addBox(const Point& center, Dyadic3DTensor& t, double scale, ColorRGB& color, bool normalize)
void GlyphGeom::addBox(const Point& center, Dyadic3DTensor& t, double scale, ColorRGB& color, bool normalize, bool showNormals, double showNormalsScale)
{
TensorGlyphBuilder builder(t, center);
builder.setColor(color);
if (normalize)
builder.normalizeTensor();
builder.scaleTensor(scale);
builder.makeTensorPositive();
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateBox(constructor_);
}

void GlyphGeom::addEllipsoid(const Point& center, Dyadic3DTensor& t, double scale, int resolution, const ColorRGB& color, bool normalize)
void GlyphGeom::addEllipsoid(const Point& center, Dyadic3DTensor& t, double scale, int resolution, const ColorRGB& color, bool normalize, bool showNormals, double showNormalsScale)
{
TensorGlyphBuilder builder(t, center);
builder.setResolution(resolution);
Expand All @@ -99,94 +107,112 @@ void GlyphGeom::addEllipsoid(const Point& center, Dyadic3DTensor& t, double scal
builder.normalizeTensor();
builder.scaleTensor(scale);
builder.makeTensorPositive();
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateEllipsoid(constructor_, false);
}

void GlyphGeom::addSuperquadricTensor(const Point& center, Dyadic3DTensor& t, double scale, int resolution,
const ColorRGB& color, bool normalize, double emphasis)
const ColorRGB& color, bool normalize, double emphasis, bool showNormals, double showNormalsScale)
{
TensorGlyphBuilder builder(t, center);
builder.setResolution(resolution);
builder.setColor(color);
if (normalize)
builder.normalizeTensor();
builder.scaleTensor(scale);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateSuperquadricTensor(constructor_, emphasis);
}

void GlyphGeom::addSuperquadricSurface(const Point& center, Dyadic3DTensor& t, double scale, int resolution,
const ColorRGB& color, double A, double B)
const ColorRGB& color, double A, double B, bool showNormals, double showNormalsScale)
{
TensorGlyphBuilder builder(t, center);
builder.setResolution(resolution);
builder.setColor(color);
builder.scaleTensor(scale);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateSuperquadricSurface(constructor_, A, B);
}

void GlyphGeom::addCylinder(const Point& p1, const Point& p2, double radius, int resolution,
const ColorRGB& color1, const ColorRGB& color2,
bool renderBase1, bool renderBase2)
bool showNormals, double showNormalsScale, bool renderBase1, bool renderBase2)
{
VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setColor2(color2);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateCylinder(constructor_, radius, radius, renderBase1, renderBase2);
}

void GlyphGeom::addCylinder(const Point& p1, const Point& p2, double radius1, double radius2,
int resolution, const ColorRGB& color1, const ColorRGB& color2,
bool renderBase1, bool renderBase2)
bool showNormals, double showNormalsScale, bool renderBase1, bool renderBase2)
{
VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setColor2(color2);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateCylinder(constructor_, radius1, radius2, renderBase1, renderBase2);
}

void GlyphGeom::addDisk(const Point& p1, const Point& p2, double radius, int resolution,
const ColorRGB& color1, const ColorRGB& color2)
const ColorRGB& color1, const ColorRGB& color2, bool showNormals, double showNormalsScale)
{
VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setColor2(color2);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateCylinder(constructor_, radius, radius, true, true);
}

void GlyphGeom::addTorus(const Point& p1, const Point& p2, double major_radius, double minor_radius, int resolution,
const ColorRGB& color1, const ColorRGB&)
const ColorRGB& color1, const ColorRGB&, bool showNormals, double showNormalsScale)
{
VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateTorus(constructor_, major_radius, minor_radius);
}

void GlyphGeom::addCone(const Point& p1, const Point& p2, double radius, int resolution,
bool render_base, const ColorRGB& color1, const ColorRGB& color2)
bool render_base, const ColorRGB& color1, const ColorRGB& color2, bool showNormals, double showNormalsScale)
{
VectorGlyphBuilder builder(p1, p2);
builder.setResolution(resolution);
builder.setColor1(color1);
builder.setColor2(color2);
builder.setShowNormals(showNormals);
builder.setShowNormalsScale(showNormalsScale);
builder.generateCone(constructor_, radius, render_base);
}

void GlyphGeom::addClippingPlane(const Point& p1, const Point& p2,
const Point& p3, const Point& p4, double radius, int resolution,
const ColorRGB& color1, const ColorRGB& color2)
{
addSphere(p1, radius, resolution, color1);
addSphere(p2, radius, resolution, color1);
addSphere(p3, radius, resolution, color1);
addSphere(p4, radius, resolution, color1);
addCylinder(p1, p2, radius, resolution, color1, color2);
addCylinder(p2, p3, radius, resolution, color1, color2);
addCylinder(p3, p4, radius, resolution, color1, color2);
addCylinder(p4, p1, radius, resolution, color1, color2);
const bool showNormals = false;
const double showNormalsScale = 0.0;
addSphere(p1, radius, resolution, color1, showNormals, showNormalsScale);
addSphere(p2, radius, resolution, color1, showNormals, showNormalsScale);
addSphere(p3, radius, resolution, color1, showNormals, showNormalsScale);
addSphere(p4, radius, resolution, color1, showNormals, showNormalsScale);
addCylinder(p1, p2, radius, resolution, color1, color2, showNormals, showNormalsScale);
addCylinder(p2, p3, radius, resolution, color1, color2, showNormals, showNormalsScale);
addCylinder(p3, p4, radius, resolution, color1, color2, showNormals, showNormalsScale);
addCylinder(p4, p1, radius, resolution, color1, color2, showNormals, showNormalsScale);
}

void GlyphGeom::addPlane(const Point& p1, const Point& p2,
Expand Down Expand Up @@ -214,8 +240,9 @@ void GlyphGeom::addPoint(const Point& p, const ColorRGB& color)
constructor_.addPoint(Vector(p), color);
}

void GlyphGeom::generateSphere(const Point& center, double radius, int resolution, const ColorRGB& color)
void GlyphGeom::generateSphere(const Point& center, double radius, int resolution, const ColorRGB& color, bool showNormals, double showNormalsScale)
{
const auto prim = SpireIBO::PRIMITIVE::TRIANGLES;
if (resolution < 3) resolution = 3;
if (radius < 0) radius = 1.0;
double theta_inc = M_PI / resolution;
Expand All @@ -228,39 +255,47 @@ void GlyphGeom::generateSphere(const Point& center, double radius, int resolutio
for(int u = 0; u <= 2*resolution; ++u)
{
double theta = u * theta_inc;
Vector p1 = Vector(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
Vector p2 = Vector(sin(theta) * cos(phi + phi_inc), sin(theta) * sin(phi + phi_inc), cos(theta));

constructor_.setOffset();
constructor_.addVertex(radius * p1 + Vector(center), p1, color);
constructor_.addVertex(radius * p2 + Vector(center), p2, color);
Vector n1 = Vector(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
Vector n2 = Vector(sin(theta) * cos(phi + phi_inc), sin(theta) * sin(phi + phi_inc), cos(theta));
Vector p1 = radius * n1 + Vector(center);
Vector p2 = radius * n2 + Vector(center);

constructor_.setOffset(prim);
constructor_.addVertex(prim, p1, n1, color);
constructor_.addVertex(prim, p2, n2, color);
if (showNormals)
{
constructor_.addLine(p1, showNormalsScale * n1 + p1, color, color);
constructor_.addLine(p2, showNormalsScale * n2 + p2, color, color);
}

int v1 = 1, v2 = 2;
if(u < resolution)
std::swap(v1, v2);

constructor_.addIndicesToOffset(0, v1, v2);
constructor_.addIndicesToOffset(v2, v1, 3);
constructor_.addIndicesToOffset(prim, 0, v1, v2);
constructor_.addIndicesToOffset(prim, v2, v1, 3);
}
constructor_.popIndicesNTimes(6);
constructor_.popIndicesNTimes(prim, 6);
}
}

void GlyphGeom::generatePlane(const Point& p1, const Point& p2,
const Point& p3, const Point& p4, const ColorRGB& color)
{
const auto prim = SpireIBO::PRIMITIVE::TRIANGLES;
Vector n1 = Cross(p2 - p1, p4 - p1).normal();
Vector n2 = Cross(p3 - p2, p1 - p2).normal();
Vector n3 = Cross(p4 - p3, p2 - p3).normal();
Vector n4 = Cross(p1 - p4, p3 - p4).normal();

constructor_.setOffset();
constructor_.setOffset(prim);

constructor_.addVertex(Vector(p1), n1, color);
constructor_.addVertex(Vector(p2), n2, color);
constructor_.addVertex(Vector(p3), n3, color);
constructor_.addVertex(Vector(p4), n4, color);
constructor_.addVertex(prim, Vector(p1), n1, color);
constructor_.addVertex(prim, Vector(p2), n2, color);
constructor_.addVertex(prim, Vector(p3), n3, color);
constructor_.addVertex(prim, Vector(p4), n4, color);

constructor_.addIndicesToOffset(0, 1, 2);
constructor_.addIndicesToOffset(2, 3, 0);
constructor_.addIndicesToOffset(prim, 0, 1, 2);
constructor_.addIndicesToOffset(prim, 2, 3, 0);
}
Loading