Add outline rendering.

This commit is contained in:
James Grogan 2022-11-18 09:43:22 +00:00
parent f04d86e0ad
commit a20c0183df
20 changed files with 291 additions and 64 deletions

View file

@ -38,3 +38,26 @@ void AbstractMesh::scale(double scaleX, double scaleY)
node->scale(scaleX, scaleY);
}
}
void AbstractMesh::transform(const Transform& transform)
{
auto scaleX = transform.getScaleX();
auto scaleY = transform.getScaleY();
scale(scaleX, scaleY);
translate(transform.getLocation());
}
void AbstractMesh::translate(double offsetX, double offsetY, double offsetZ)
{
for (auto& node : mNodes)
{
node->translate(offsetX, offsetY, offsetZ);
}
}
void AbstractMesh::translate(const Point& offset)
{
translate(offset.getX(), offset.getY(), offset.getZ());
}

View file

@ -2,6 +2,7 @@
#include "Point.h"
#include "Node.h"
#include "Transform.h"
#include <memory>
#include <vector>
@ -49,6 +50,12 @@ public:
void scale(double scaleX, double scaleY);
void translate(const Point& offset);
void translate(double offsetX, double offsetY, double offsetZ = 0.0);
void transform(const Transform& transform);
protected:
std::unordered_map<std::string, std::vector<double> > mVectorAttributes;
VecNodes mNodes;

View file

@ -7,6 +7,7 @@ list(APPEND mesh_LIB_INCLUDES
Node.cpp
QuadMesh.cpp
TriMesh.cpp
LineMesh.cpp
MeshPrimitives.cpp
MeshBuilder.cpp)

25
src/mesh/LineMesh.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "LineMesh.h"
#include "Edge.h"
LineMesh::~LineMesh()
{
}
void LineMesh::populate(VecNodes& nodes, VecEdges& edges)
{
mNodes = std::move(nodes);
mEdges = std::move(edges);
}
std::vector<unsigned> LineMesh::getEdgeNodeIds() const
{
std::vector<unsigned> ids(2*mEdges.size());
for(std::size_t idx=0; idx<mEdges.size(); idx++)
{
ids[2*idx] = mEdges[idx]->getNode0Id();
ids[2*idx + 1] = mEdges[idx]->getNode1Id();
}
return ids;
}

30
src/mesh/LineMesh.h Normal file
View file

@ -0,0 +1,30 @@
#pragma once
#include "AbstractMesh.h"
#include <string>
#include <unordered_map>
class Edge;
using EdgePtr = std::unique_ptr<Edge>;
using VecEdges = std::vector<EdgePtr>;
class LineMesh : public AbstractMesh
{
public:
LineMesh() = default;
~LineMesh();
void populate(VecNodes& nodes, VecEdges& edges);
std::vector<unsigned> getEdgeNodeIds() const;
MeshType getType() const override
{
return MeshType::LINE;
}
private:
VecEdges mEdges;
};

View file

@ -33,3 +33,23 @@ std::unique_ptr<TriMesh> MeshBuilder::buildTriMesh(const VecPoints& locations, c
mesh->populate(nodes, edges, faces);
return mesh;
}
std::unique_ptr<LineMesh> MeshBuilder::buildLineMesh(const VecPoints& locations, const EdgeIds& edgeIds)
{
auto mesh = std::make_unique<LineMesh>();
VecNodes nodes(locations.size());
for (std::size_t idx=0; idx<locations.size(); idx++)
{
nodes[idx] = Node::Create(locations[idx], idx);
}
VecEdges edges(edgeIds.size());
for (std::size_t idx=0; idx<edgeIds.size(); idx++)
{
edges[idx] = Edge::Create(nodes[edgeIds[idx].first].get(), nodes[edgeIds[idx].second].get(), idx);
}
mesh->populate(nodes, edges);
return mesh;
}

View file

@ -1,8 +1,11 @@
#pragma once
#include "TriMesh.h"
#include "LineMesh.h"
#include "Point.h"
#include <memory>
using EdgeIds = std::vector<std::pair<unsigned, unsigned> >;
using FaceIds = std::vector<std::vector<unsigned> >;
using VecPoints = std::vector<Point>;
@ -11,4 +14,6 @@ class MeshBuilder
{
public:
static std::unique_ptr<TriMesh> buildTriMesh(const VecPoints& locations, const EdgeIds& edgeIds, const FaceIds& faceIds);
static std::unique_ptr<LineMesh> buildLineMesh(const VecPoints& locations, const EdgeIds& edgeIds);
};

View file

@ -1,18 +1,16 @@
#include "MeshPrimitives.h"
#include "MeshBuilder.h"
#include <iostream>
std::unique_ptr<TriMesh> MeshPrimitives::build(const Rectangle& rectangle)
std::unique_ptr<TriMesh> MeshPrimitives::buildRectangleAsTriMesh()
{
const auto bottom_left = rectangle.getLocation();
const auto width = rectangle.getWidth();
const auto height = rectangle.getHeight();
VecPoints locations = {
bottom_left,
Point(bottom_left, width, 0),
Point(bottom_left, width, height),
Point(bottom_left, 0, height)
{0, 0},
{1, 0},
{1, 1},
{0, 1}
};
EdgeIds edge_ids = {
@ -31,14 +29,33 @@ std::unique_ptr<TriMesh> MeshPrimitives::build(const Rectangle& rectangle)
return MeshBuilder::buildTriMesh(locations, edge_ids, face_ids);
}
std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGrid(unsigned numX, unsigned numY)
std::unique_ptr<LineMesh> MeshPrimitives::buildRectangleAsLineMesh()
{
VecPoints locations = {
{0, 0},
{1, 0},
{1, 1},
{0, 1}
};
EdgeIds edge_ids = {
{0, 1},
{1, 2},
{2, 3},
{3, 0}
};
return MeshBuilder::buildLineMesh(locations, edge_ids);
}
std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGridAsTriMesh(unsigned numX, unsigned numY)
{
double delta_x = 1.0/double(numX);
double delta_y = 1.0/double(numY);
VecPoints locations (4 * numX * numY);
double offset_x = delta_x;
double offset_y = delta_y;
double offset_x = delta_x/2.0;
double offset_y = delta_y/2.0;
for (unsigned idx=0; idx<numY; idx++)
{
for(unsigned jdx=0; jdx<numX; jdx++)
@ -53,9 +70,10 @@ std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGrid(unsigned numX, unsign
locations[id_offset + 1] = Point(locX1, locY0);
locations[id_offset + 2] = Point(locX1, locY1);
locations[id_offset + 3] = Point(locX0, locY1);
offset_x += delta_x;
}
offset_x = delta_x;
offset_x = delta_x/2.0;
offset_y += delta_y;
}
@ -88,3 +106,49 @@ std::unique_ptr<TriMesh> MeshPrimitives::buildExplodedGrid(unsigned numX, unsign
return MeshBuilder::buildTriMesh(locations, edge_ids, face_ids);
}
std::unique_ptr<LineMesh> MeshPrimitives::buildExplodedGridAsLineMesh(unsigned numX, unsigned numY)
{
double delta_x = 1.0/double(numX);
double delta_y = 1.0/double(numY);
VecPoints locations (4 * numX * numY);
double offset_x = delta_x/2.0;
double offset_y = delta_y/2.0;
for (unsigned idx=0; idx<numY; idx++)
{
for(unsigned jdx=0; jdx<numX; jdx++)
{
auto locX0 = offset_x - delta_x/2.0;
auto locX1 = offset_x + delta_x/2.0;
auto locY0 = offset_y - delta_y/2.0;
auto locY1 = offset_y + delta_y/2.0;
auto id_offset = 4* (jdx + numX*idx);
locations[id_offset] = Point(locX0, locY0);
locations[id_offset + 1] = Point(locX1, locY0);
locations[id_offset + 2] = Point(locX1, locY1);
locations[id_offset + 3] = Point(locX0, locY1);
offset_x += delta_x;
}
offset_x = delta_x/2.0;
offset_y += delta_y;
}
EdgeIds edge_ids(4 * numX * numY);
for (unsigned idx=0; idx<numY; idx++)
{
for(unsigned jdx=0; jdx<numX; jdx++)
{
unsigned node_offset = 4 * (jdx + numX * idx);
auto id_offset = 4* (jdx + numX*idx);
edge_ids[id_offset] = {node_offset, node_offset + 1};
edge_ids[id_offset + 1] = {node_offset + 1, node_offset + 2};
edge_ids[id_offset + 2] = {node_offset + 2, node_offset + 3};
edge_ids[id_offset + 3] = {node_offset + 3, node_offset};
}
}
return MeshBuilder::buildLineMesh(locations, edge_ids);
}

View file

@ -1,21 +1,17 @@
#pragma once
#include "Rectangle.h"
#include "TriMesh.h"
#include "Node.h"
#include "Edge.h"
#include "MeshBuilder.h"
#include "AbstractGeometricItem.h"
#include <tuple>
#include "LineMesh.h"
class MeshPrimitives
{
public:
static std::unique_ptr<TriMesh> build(const Rectangle& rectangle);
static std::unique_ptr<TriMesh> buildRectangleAsTriMesh();
static std::unique_ptr<TriMesh> buildExplodedGrid(unsigned numX, unsigned numY);
static std::unique_ptr<LineMesh> buildRectangleAsLineMesh();
static std::unique_ptr<TriMesh> buildExplodedGridAsTriMesh(unsigned numX, unsigned numY);
static std::unique_ptr<LineMesh> buildExplodedGridAsLineMesh(unsigned numX, unsigned numY);
};

View file

@ -34,6 +34,11 @@ public:
mPoint.scale(x, y);
}
void translate(double x, double y, double z = 0.0)
{
mPoint.translate(x, y, z);
}
private:
std::unordered_map<std::string, std::vector<double> > mVectorAttributes;
unsigned mIndex{0};