carla/LibCarla/source/third-party/marchingcube/MeshReconstruction.h

107 lines
3.4 KiB
C
Raw Normal View History

Aaron/digitaltwinsbumps (#6346) * Added way to download files from overpass api * Save downloaded text to a file * Changed log type for File manipulation * Online process files when request is valid * Correct file format * Correct file format for UE4 class * Fix compilation issue due to name change * Create Widget with OpenFileDialogFunctionality * Step xodr to map completed * Generated Static meshes and replace procedural actors * Created and saved assets during road generation * Formatted file correctly * OSM To ODR broguht to UE4 * Full pipeline working on linux * Added osm2odr support in editor in Windos * Added Widget to CartaTools * Fixed Linux compilation error * Added Carla Game Instance included to avoid compilation error * Osm Renderer Tool dummy version * Server creates SVG files when client request it * SVG creation and rasterization - libraries integration * Server size working * Added Simplify to LibCarla, Added timers to measure time generation. Add mesh deformation during road creation. * Fixed mesh format translations * Trying to paint bitmap into the UTexture * Bitmap sent and drawn in widget texture with bugs * Map bitmap shown on widget * Concated meshes generated in the same lane to avoid errors during simplification * Navigation added to widget * Avoid Simplify to remove border vertices and try to parallel assets creation process * Road Generation 0.1 version ready * Removing Engine Association, Formatting CarlaTools Build dependencies * Change container type of generated procedural mesh componetns to be supported by UPROPERTY * Fixed indices jumping by two * Added in separate thread junctions generation * Started dynamic database creation * Dynamic database creation temporally removed * First step of merge. Coords of bottom left corner and top right corner * Libraries added to build system * Git ignore for osmrenderer to avoid ThirdParties directory to be tracked * Lat and Lon coords for corners sent from server to client * Transformed to local coords meshes' vertices' coords * Coords format error fixed * Saving xodr and osm files inside of OpenDrive folder * Widget fixed * UI design improved * WIP Windows build system for osm-world-renderer * Socket implementation replaced by boost asio framework in osmrenderer * Added multithreaded simplification of meshes * Build system adapted to wndows * Headers fixed to avoid windows specific heraders compilation * Remove warnings * Added widget to import building from houdini * Added origin latitude and longituda to OSM to OpenDRIVE conversion functions. Fixed Houdini importer widgets. * Add Houdini plugin download to the build system * Moved houdini blueprint. Houdini plugin now dowloads by default * Added houdini download for windows * OpenDriveToMap Now is a UOBject instead of widget * Added Lane mark generation. * Roads materials and distance field scale set to 0 * M_PI macro fixed for windows osm-renderer build system * Added Lane Marking generation * Fixed compilation issue related with std pair non copyable lane * Fix bug where different lanes were concating. Fix bug where end of roadmark was creating an artifact * Lanes Marks material assignation * Fix compilation issue and reading from not valid memory crash * Middle Lane mark duplication bug fixed - temp solution * Added bumps along road * Adding marchingcubes library and added to create junctions * Added junctions generations using marching cube and smoothed * Fixed linux compilation and removed couple warnings * Using previous algorithim for two road connections * Code cleanup * Remove debug state * Format Files * Format third parties files * Spaces removal * Fix code format * Removing unnecesary spaces * Format fixing * Fixed spaces --------- Co-authored-by: aollero <aollero@cvc.uab.cat> Co-authored-by: aollero <adriollero@gmail.com> Co-authored-by: Axel <axellopez92@outlook.com>
2023-04-11 16:46:55 +08:00
#pragma once
#include "DataStructs.h"
#include "Cube.h"
#include "Triangulation.h"
namespace MeshReconstruction
{
/// Reconstructs a triangle mesh from a given signed distance function using <a href="https://en.wikipedia.org/wiki/Marching_cubes">Marching Cubes</a>.
/// @param sdf The <a href="http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm">Signed Distance Function</a>.
/// @param domain Domain of reconstruction.
/// @returns The reconstructed mesh.
Mesh MarchCube(
Fun3s const &sdf,
Rect3 const &domain);
/// Reconstructs a triangle mesh from a given signed distance function using <a href="https://en.wikipedia.org/wiki/Marching_cubes">Marching Cubes</a>.
/// @param sdf The <a href="http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm">Signed Distance Function</a>.
/// @param domain Domain of reconstruction.
/// @param cubeSize Size of marching cubes. Smaller cubes yields meshes of higher resolution.
/// @param isoLevel Level set of the SDF for which triangulation should be done. Changing this value moves the reconstructed surface.
/// @param sdfGrad Gradient of the SDF which yields the vertex normals of the reconstructed mesh. If none is provided a numerical approximation is used.
/// @returns The reconstructed mesh.
Mesh MarchCube(
Fun3s const &sdf,
Rect3 const &domain,
Vec3 const &cubeSize,
double isoLevel = 0,
Fun3v sdfGrad = nullptr);
}
using namespace MeshReconstruction;
using namespace std;
// Adapted from here: http://paulbourke.net/geometry/polygonise/
namespace
{
Vec3 NumGrad(Fun3s const &f, Vec3 const &p)
{
auto const Eps = 1e-6;
Vec3 epsX{Eps, 0, 0}, epsY{0, Eps, 0}, epsZ{0, 0, Eps};
auto gx = (f(p + epsX) - f(p - epsX)) / 2;
auto gy = (f(p + epsY) - f(p - epsY)) / 2;
auto gz = (f(p + epsZ) - f(p - epsZ)) / 2;
return {gx, gy, gz};
}
}
Mesh MeshReconstruction::MarchCube(Fun3s const &sdf, Rect3 const &domain)
{
auto const NumCubes = 50;
auto cubeSize = domain.size * (1.0 / NumCubes);
return MarchCube(sdf, domain, cubeSize);
}
Mesh MeshReconstruction::MarchCube(
Fun3s const &sdf,
Rect3 const &domain,
Vec3 const &cubeSize,
double isoLevel,
Fun3v sdfGrad)
{
// Default value.
sdfGrad = sdfGrad == nullptr
? [&sdf](Vec3 const &p)
{ return NumGrad(sdf, p); }
: sdfGrad;
auto const NumX = static_cast<int>(ceil(domain.size.x / cubeSize.x));
auto const NumY = static_cast<int>(ceil(domain.size.y / cubeSize.y));
auto const NumZ = static_cast<int>(ceil(domain.size.z / cubeSize.z));
auto const HalfCubeDiag = cubeSize.Norm() / 2.0;
auto const HalfCubeSize = cubeSize * 0.5;
Mesh mesh;
for (auto ix = 0; ix < NumX; ++ix)
{
auto x = domain.min.x + ix * cubeSize.x;
for (auto iy = 0; iy < NumY; ++iy)
{
auto y = domain.min.y + iy * cubeSize.y;
for (auto iz = 0; iz < NumZ; ++iz)
{
auto z = domain.min.z + iz * cubeSize.z;
Vec3 min{x, y, z};
// Process only if cube lies within narrow band around surface.
auto cubeCenter = min + HalfCubeSize;
auto dist = abs(sdf(cubeCenter) - isoLevel);
if (dist > HalfCubeDiag)
continue;
Cube cube({min, cubeSize}, sdf);
auto intersect = cube.Intersect(isoLevel);
Triangulate(intersect, sdfGrad, mesh);
}
}
}
return mesh;
}