Thekla: Revert to state before clang-format changes

The file was wrongly formatted in 8b01b2e85c,
for thirdparty code we keep the upstream style *if* we need to make changes at all.
This commit is contained in:
Rémi Verschelde 2017-12-20 00:15:52 +01:00
parent ff04c07912
commit eb39b7f5de
3 changed files with 1218 additions and 1130 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,52 +5,59 @@
#define NV_MESH_ATLASPACKER_H
#include "nvcore/RadixSort.h"
#include "nvmath/Vector.h"
#include "nvmath/Random.h"
#include "nvimage/BitMap.h"
#include "nvimage/Image.h"
#include "nvmath/Random.h"
#include "nvmath/Vector.h"
#include "nvmesh/nvmesh.h"
namespace nv {
class Atlas;
class Chart;
struct AtlasPacker {
AtlasPacker(Atlas *atlas);
~AtlasPacker();
namespace nv
{
class Atlas;
class Chart;
void packCharts(int quality, float texelArea, bool blockAligned, bool conservative);
float computeAtlasUtilization() const;
struct AtlasPacker
{
AtlasPacker(Atlas * atlas);
~AtlasPacker();
private:
void findChartLocation(int quality, const BitMap *bitmap, Vector2::Arg extents, int w, int h, int *best_x, int *best_y, int *best_w, int *best_h, int *best_r);
void findChartLocation_bruteForce(const BitMap *bitmap, Vector2::Arg extents, int w, int h, int *best_x, int *best_y, int *best_w, int *best_h, int *best_r);
void findChartLocation_random(const BitMap *bitmap, Vector2::Arg extents, int w, int h, int *best_x, int *best_y, int *best_w, int *best_h, int *best_r, int minTrialCount);
void packCharts(int quality, float texelArea, bool blockAligned, bool conservative);
float computeAtlasUtilization() const;
void drawChartBitmapDilate(const Chart *chart, BitMap *bitmap, int padding);
void drawChartBitmap(const Chart *chart, BitMap *bitmap, const Vector2 &scale, const Vector2 &offset);
private:
bool canAddChart(const BitMap *bitmap, int w, int h, int x, int y, int r);
void addChart(const BitMap *bitmap, int w, int h, int x, int y, int r, Image *debugOutput);
//void checkCanAddChart(const Chart * chart, int w, int h, int x, int y, int r);
void addChart(const Chart *chart, int w, int h, int x, int y, int r, Image *debugOutput);
void findChartLocation(int quality, const BitMap * bitmap, Vector2::Arg extents, int w, int h, int * best_x, int * best_y, int * best_w, int * best_h, int * best_r);
void findChartLocation_bruteForce(const BitMap * bitmap, Vector2::Arg extents, int w, int h, int * best_x, int * best_y, int * best_w, int * best_h, int * best_r);
void findChartLocation_random(const BitMap * bitmap, Vector2::Arg extents, int w, int h, int * best_x, int * best_y, int * best_w, int * best_h, int * best_r, int minTrialCount);
static bool checkBitsCallback(void *param, int x, int y, Vector3::Arg bar, Vector3::Arg dx, Vector3::Arg dy, float coverage);
static bool setBitsCallback(void *param, int x, int y, Vector3::Arg bar, Vector3::Arg dx, Vector3::Arg dy, float coverage);
void drawChartBitmapDilate(const Chart * chart, BitMap * bitmap, int padding);
void drawChartBitmap(const Chart * chart, BitMap * bitmap, const Vector2 & scale, const Vector2 & offset);
bool canAddChart(const BitMap * bitmap, int w, int h, int x, int y, int r);
void addChart(const BitMap * bitmap, int w, int h, int x, int y, int r, Image * debugOutput);
//void checkCanAddChart(const Chart * chart, int w, int h, int x, int y, int r);
void addChart(const Chart * chart, int w, int h, int x, int y, int r, Image * debugOutput);
private:
Atlas *m_atlas;
BitMap m_bitmap;
//Image m_debug_bitmap;
RadixSort m_radix;
static bool checkBitsCallback(void * param, int x, int y, Vector3::Arg bar, Vector3::Arg dx, Vector3::Arg dy, float coverage);
static bool setBitsCallback(void * param, int x, int y, Vector3::Arg bar, Vector3::Arg dx, Vector3::Arg dy, float coverage);
uint m_width;
uint m_height;
private:
MTRand m_rand;
};
Atlas * m_atlas;
BitMap m_bitmap;
Image m_debug_bitmap;
RadixSort m_radix;
} // namespace nv
uint m_width;
uint m_height;
MTRand m_rand;
};
} // nv namespace
#endif // NV_MESH_ATLASPACKER_H

View file

@ -4,8 +4,8 @@
#include <cfloat>
#include "nvmesh/halfedge/Edge.h"
#include "nvmesh/halfedge/Face.h"
#include "nvmesh/halfedge/Mesh.h"
#include "nvmesh/halfedge/Face.h"
#include "nvmesh/halfedge/Vertex.h"
#include "nvmesh/param/Atlas.h"
@ -14,255 +14,258 @@
#include "nvcore/Array.inl"
#include <stdio.h>
using namespace Thekla;
using namespace nv;
inline Atlas_Output_Mesh *set_error(Atlas_Error *error, Atlas_Error code) {
if (error) *error = code;
return NULL;
inline Atlas_Output_Mesh * set_error(Atlas_Error * error, Atlas_Error code) {
if (error) *error = code;
return NULL;
}
static void input_to_mesh(const Atlas_Input_Mesh *input, HalfEdge::Mesh *mesh, Atlas_Error *error) {
Array<uint> canonicalMap;
canonicalMap.reserve(input->vertex_count);
for (int i = 0; i < input->vertex_count; i++) {
const Atlas_Input_Vertex &input_vertex = input->vertex_array[i];
const float *pos = input_vertex.position;
const float *nor = input_vertex.normal;
const float *tex = input_vertex.uv;
static void input_to_mesh(const Atlas_Input_Mesh * input, HalfEdge::Mesh * mesh, Atlas_Error * error) {
HalfEdge::Vertex *vertex = mesh->addVertex(Vector3(pos[0], pos[1], pos[2]));
vertex->nor.set(nor[0], nor[1], nor[2]);
vertex->tex.set(tex[0], tex[1]);
Array<uint> canonicalMap;
canonicalMap.reserve(input->vertex_count);
canonicalMap.append(input_vertex.first_colocal);
}
for (int i = 0; i < input->vertex_count; i++) {
const Atlas_Input_Vertex & input_vertex = input->vertex_array[i];
const float * pos = input_vertex.position;
const float * nor = input_vertex.normal;
const float * tex = input_vertex.uv;
mesh->linkColocalsWithCanonicalMap(canonicalMap);
HalfEdge::Vertex * vertex = mesh->addVertex(Vector3(pos[0], pos[1], pos[2]));
vertex->nor.set(nor[0], nor[1], nor[2]);
vertex->tex.set(tex[0], tex[1]);
const int face_count = input->face_count;
canonicalMap.append(input_vertex.first_colocal);
}
int non_manifold_faces = 0;
for (int i = 0; i < face_count; i++) {
const Atlas_Input_Face &input_face = input->face_array[i];
mesh->linkColocalsWithCanonicalMap(canonicalMap);
int v0 = input_face.vertex_index[0];
int v1 = input_face.vertex_index[1];
int v2 = input_face.vertex_index[2];
HalfEdge::Face *face = mesh->addFace(v0, v1, v2);
if (face != NULL) {
face->material = input_face.material_index;
} else {
non_manifold_faces++;
}
}
const int face_count = input->face_count;
mesh->linkBoundary();
int non_manifold_faces = 0;
for (int i = 0; i < face_count; i++) {
const Atlas_Input_Face & input_face = input->face_array[i];
if (non_manifold_faces != 0 && error != NULL) {
*error = Atlas_Error_Invalid_Mesh_Non_Manifold;
}
int v0 = input_face.vertex_index[0];
int v1 = input_face.vertex_index[1];
int v2 = input_face.vertex_index[2];
HalfEdge::Face * face = mesh->addFace(v0, v1, v2);
if (face != NULL) {
face->material = input_face.material_index;
}
else {
non_manifold_faces++;
}
}
mesh->linkBoundary();
if (non_manifold_faces != 0 && error != NULL) {
*error = Atlas_Error_Invalid_Mesh_Non_Manifold;
}
}
static Atlas_Output_Mesh *mesh_atlas_to_output(const HalfEdge::Mesh *mesh, const Atlas &atlas, Atlas_Error *error) {
static Atlas_Output_Mesh * mesh_atlas_to_output(const HalfEdge::Mesh * mesh, const Atlas & atlas, Atlas_Error * error) {
Atlas_Output_Mesh *output = new Atlas_Output_Mesh;
Atlas_Output_Mesh * output = new Atlas_Output_Mesh;
const MeshCharts *charts = atlas.meshAt(0);
const MeshCharts * charts = atlas.meshAt(0);
// Allocate vertices.
const int vertex_count = charts->vertexCount();
output->vertex_count = vertex_count;
output->vertex_array = new Atlas_Output_Vertex[vertex_count];
// Allocate vertices.
const int vertex_count = charts->vertexCount();
output->vertex_count = vertex_count;
output->vertex_array = new Atlas_Output_Vertex[vertex_count];
int w = 0;
int h = 0;
int w = 0;
int h = 0;
// Output vertices.
const int chart_count = charts->chartCount();
for (int i = 0; i < chart_count; i++) {
const Chart *chart = charts->chartAt(i);
uint vertexOffset = charts->vertexCountBeforeChartAt(i);
// Output vertices.
const int chart_count = charts->chartCount();
for (int i = 0; i < chart_count; i++) {
const Chart * chart = charts->chartAt(i);
uint vertexOffset = charts->vertexCountBeforeChartAt(i);
const uint chart_vertex_count = chart->vertexCount();
for (uint v = 0; v < chart_vertex_count; v++) {
Atlas_Output_Vertex &output_vertex = output->vertex_array[vertexOffset + v];
const uint chart_vertex_count = chart->vertexCount();
for (uint v = 0; v < chart_vertex_count; v++) {
Atlas_Output_Vertex & output_vertex = output->vertex_array[vertexOffset + v];
uint original_vertex = chart->mapChartVertexToOriginalVertex(v);
output_vertex.xref = original_vertex;
uint original_vertex = chart->mapChartVertexToOriginalVertex(v);
output_vertex.xref = original_vertex;
Vector2 uv = chart->chartMesh()->vertexAt(v)->tex;
output_vertex.uv[0] = uv.x;
output_vertex.uv[1] = uv.y;
w = max(w, ftoi_ceil(uv.x));
h = max(h, ftoi_ceil(uv.y));
}
}
Vector2 uv = chart->chartMesh()->vertexAt(v)->tex;
output_vertex.uv[0] = uv.x;
output_vertex.uv[1] = uv.y;
w = max(w, ftoi_ceil(uv.x));
h = max(h, ftoi_ceil(uv.y));
}
}
const int face_count = mesh->faceCount();
output->index_count = face_count * 3;
output->index_array = new int[face_count * 3];
const int face_count = mesh->faceCount();
output->index_count = face_count * 3;
output->index_array = new int[face_count * 3];
int face_ofs = 0;
// Set face indices.
for (int f = 0; f < face_count; f++) {
uint c = charts->faceChartAt(f);
uint i = charts->faceIndexWithinChartAt(f);
uint vertexOffset = charts->vertexCountBeforeChartAt(c);
// Set face indices.
for (int f = 0; f < face_count; f++) {
uint c = charts->faceChartAt(f);
uint i = charts->faceIndexWithinChartAt(f);
uint vertexOffset = charts->vertexCountBeforeChartAt(c);
const Chart *chart = charts->chartAt(c);
nvDebugCheck(chart->faceAt(i) == f);
const Chart * chart = charts->chartAt(c);
nvDebugCheck(chart->faceAt(i) == f);
if (i >= chart->chartMesh()->faceCount()) {
printf("WARNING: Faces may be missing in the final vertex, which could not be packed\n");
const HalfEdge::Face * face = chart->chartMesh()->faceAt(i);
const HalfEdge::Edge * edge = face->edge;
continue;
}
const HalfEdge::Face *face = chart->chartMesh()->faceAt(i);
const HalfEdge::Edge *edge = face->edge;
output->index_array[3*f+0] = vertexOffset + edge->vertex->id;
output->index_array[3*f+1] = vertexOffset + edge->next->vertex->id;
output->index_array[3*f+2] = vertexOffset + edge->next->next->vertex->id;
}
output->index_array[3 * face_ofs + 0] = vertexOffset + edge->vertex->id;
output->index_array[3 * face_ofs + 1] = vertexOffset + edge->next->vertex->id;
output->index_array[3 * face_ofs + 2] = vertexOffset + edge->next->next->vertex->id;
face_ofs++;
}
*error = Atlas_Error_Success;
output->atlas_width = w;
output->atlas_height = h;
output->index_count = face_ofs * 3;
*error = Atlas_Error_Success;
output->atlas_width = w;
output->atlas_height = h;
return output;
return output;
}
void Thekla::atlas_set_default_options(Atlas_Options *options) {
if (options != NULL) {
// These are the default values we use on The Witness.
options->charter = Atlas_Charter_Default;
options->charter_options.witness.proxy_fit_metric_weight = 2.0f;
options->charter_options.witness.roundness_metric_weight = 0.01f;
options->charter_options.witness.straightness_metric_weight = 6.0f;
options->charter_options.witness.normal_seam_metric_weight = 4.0f;
options->charter_options.witness.texture_seam_metric_weight = 0.5f;
options->charter_options.witness.max_chart_area = FLT_MAX;
options->charter_options.witness.max_boundary_length = FLT_MAX;
void Thekla::atlas_set_default_options(Atlas_Options * options) {
if (options != NULL) {
// These are the default values we use on The Witness.
options->mapper = Atlas_Mapper_Default;
options->charter = Atlas_Charter_Default;
options->charter_options.witness.proxy_fit_metric_weight = 2.0f;
options->charter_options.witness.roundness_metric_weight = 0.01f;
options->charter_options.witness.straightness_metric_weight = 6.0f;
options->charter_options.witness.normal_seam_metric_weight = 4.0f;
options->charter_options.witness.texture_seam_metric_weight = 0.5f;
options->charter_options.witness.max_chart_area = FLT_MAX;
options->charter_options.witness.max_boundary_length = FLT_MAX;
options->packer = Atlas_Packer_Default;
options->packer_options.witness.packing_quality = 0;
options->packer_options.witness.texel_area = 8;
options->packer_options.witness.block_align = true;
options->packer_options.witness.conservative = false;
}
options->mapper = Atlas_Mapper_Default;
options->packer = Atlas_Packer_Default;
options->packer_options.witness.packing_quality = 0;
options->packer_options.witness.texel_area = 8;
options->packer_options.witness.block_align = true;
options->packer_options.witness.conservative = false;
}
}
Atlas_Output_Mesh *Thekla::atlas_generate(const Atlas_Input_Mesh *input, const Atlas_Options *options, Atlas_Error *error) {
// Validate args.
if (input == NULL || options == NULL || error == NULL) return set_error(error, Atlas_Error_Invalid_Args);
// Validate options.
if (options->charter != Atlas_Charter_Witness) {
return set_error(error, Atlas_Error_Invalid_Options);
}
if (options->charter == Atlas_Charter_Witness) {
// @@ Validate input options!
}
Atlas_Output_Mesh * Thekla::atlas_generate(const Atlas_Input_Mesh * input, const Atlas_Options * options, Atlas_Error * error) {
// Validate args.
if (input == NULL || options == NULL || error == NULL) return set_error(error, Atlas_Error_Invalid_Args);
if (options->mapper != Atlas_Mapper_LSCM) {
return set_error(error, Atlas_Error_Invalid_Options);
}
if (options->mapper == Atlas_Mapper_LSCM) {
// No options.
}
// Validate options.
if (options->charter != Atlas_Charter_Witness) {
return set_error(error, Atlas_Error_Invalid_Options);
}
if (options->charter == Atlas_Charter_Witness) {
// @@ Validate input options!
}
if (options->packer != Atlas_Packer_Witness) {
return set_error(error, Atlas_Error_Invalid_Options);
}
if (options->packer == Atlas_Packer_Witness) {
// @@ Validate input options!
}
if (options->mapper != Atlas_Mapper_LSCM) {
return set_error(error, Atlas_Error_Invalid_Options);
}
if (options->mapper == Atlas_Mapper_LSCM) {
// No options.
}
// Validate input mesh.
for (int i = 0; i < input->face_count; i++) {
int v0 = input->face_array[i].vertex_index[0];
int v1 = input->face_array[i].vertex_index[1];
int v2 = input->face_array[i].vertex_index[2];
if (options->packer != Atlas_Packer_Witness) {
return set_error(error, Atlas_Error_Invalid_Options);
}
if (options->packer == Atlas_Packer_Witness) {
// @@ Validate input options!
}
if (v0 < 0 || v0 >= input->vertex_count ||
v1 < 0 || v1 >= input->vertex_count ||
v2 < 0 || v2 >= input->vertex_count) {
return set_error(error, Atlas_Error_Invalid_Mesh);
}
}
// Validate input mesh.
for (int i = 0; i < input->face_count; i++) {
int v0 = input->face_array[i].vertex_index[0];
int v1 = input->face_array[i].vertex_index[1];
int v2 = input->face_array[i].vertex_index[2];
// Build half edge mesh.
AutoPtr<HalfEdge::Mesh> mesh(new HalfEdge::Mesh);
if (v0 < 0 || v0 >= input->vertex_count ||
v1 < 0 || v1 >= input->vertex_count ||
v2 < 0 || v2 >= input->vertex_count)
{
return set_error(error, Atlas_Error_Invalid_Mesh);
}
}
input_to_mesh(input, mesh.ptr(), error);
if (*error == Atlas_Error_Invalid_Mesh) {
return NULL;
}
// Build half edge mesh.
AutoPtr<HalfEdge::Mesh> mesh(new HalfEdge::Mesh);
Atlas atlas;
input_to_mesh(input, mesh.ptr(), error);
// Charter.
if (options->charter == Atlas_Charter_Extract) {
return set_error(error, Atlas_Error_Not_Implemented);
} else if (options->charter == Atlas_Charter_Witness) {
SegmentationSettings segmentation_settings;
segmentation_settings.proxyFitMetricWeight = options->charter_options.witness.proxy_fit_metric_weight;
segmentation_settings.roundnessMetricWeight = options->charter_options.witness.roundness_metric_weight;
segmentation_settings.straightnessMetricWeight = options->charter_options.witness.straightness_metric_weight;
segmentation_settings.normalSeamMetricWeight = options->charter_options.witness.normal_seam_metric_weight;
segmentation_settings.textureSeamMetricWeight = options->charter_options.witness.texture_seam_metric_weight;
segmentation_settings.maxChartArea = options->charter_options.witness.max_chart_area;
segmentation_settings.maxBoundaryLength = options->charter_options.witness.max_boundary_length;
if (*error == Atlas_Error_Invalid_Mesh) {
return NULL;
}
Array<uint> uncharted_materials;
atlas.computeCharts(mesh.ptr(), segmentation_settings, uncharted_materials);
}
Atlas atlas;
if (atlas.hasFailed())
return NULL;
// Charter.
if (options->charter == Atlas_Charter_Extract) {
return set_error(error, Atlas_Error_Not_Implemented);
}
else if (options->charter == Atlas_Charter_Witness) {
SegmentationSettings segmentation_settings;
segmentation_settings.proxyFitMetricWeight = options->charter_options.witness.proxy_fit_metric_weight;
segmentation_settings.roundnessMetricWeight = options->charter_options.witness.roundness_metric_weight;
segmentation_settings.straightnessMetricWeight = options->charter_options.witness.straightness_metric_weight;
segmentation_settings.normalSeamMetricWeight = options->charter_options.witness.normal_seam_metric_weight;
segmentation_settings.textureSeamMetricWeight = options->charter_options.witness.texture_seam_metric_weight;
segmentation_settings.maxChartArea = options->charter_options.witness.max_chart_area;
segmentation_settings.maxBoundaryLength = options->charter_options.witness.max_boundary_length;
// Mapper.
if (options->mapper == Atlas_Mapper_LSCM) {
atlas.parameterizeCharts();
}
Array<uint> uncharted_materials;
atlas.computeCharts(mesh.ptr(), segmentation_settings, uncharted_materials);
}
if (atlas.hasFailed())
return NULL;
if (atlas.hasFailed())
return NULL;
// Mapper.
if (options->mapper == Atlas_Mapper_LSCM) {
atlas.parameterizeCharts();
}
// Packer.
if (options->packer == Atlas_Packer_Witness) {
int packing_quality = options->packer_options.witness.packing_quality;
float texel_area = options->packer_options.witness.texel_area;
int block_align = options->packer_options.witness.block_align;
int conservative = options->packer_options.witness.conservative;
if (atlas.hasFailed())
return NULL;
/*float utilization =*/atlas.packCharts(packing_quality, texel_area, block_align, conservative);
}
// Packer.
if (options->packer == Atlas_Packer_Witness) {
int packing_quality = options->packer_options.witness.packing_quality;
float texel_area = options->packer_options.witness.texel_area;
int block_align = options->packer_options.witness.block_align;
int conservative = options->packer_options.witness.conservative;
if (atlas.hasFailed())
return NULL;
/*float utilization =*/ atlas.packCharts(packing_quality, texel_area, block_align, conservative);
}
if (atlas.hasFailed())
return NULL;
// Build output mesh.
return mesh_atlas_to_output(mesh.ptr(), atlas, error);
// Build output mesh.
return mesh_atlas_to_output(mesh.ptr(), atlas, error);
}
void Thekla::atlas_free(Atlas_Output_Mesh *output) {
if (output != NULL) {
delete[] output->vertex_array;
delete[] output->index_array;
delete output;
}
void Thekla::atlas_free(Atlas_Output_Mesh * output) {
if (output != NULL) {
delete [] output->vertex_array;
delete [] output->index_array;
delete output;
}
}