2014-05-10 17:16:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011-2014 hasufell
|
|
|
|
*
|
|
|
|
* This file is part of a hasufell project.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation version 2 of the License only.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-10 17:33:54 +00:00
|
|
|
/**
|
|
|
|
* @file half_edge.c
|
|
|
|
* This file provides operations on half-edge data structures
|
|
|
|
* which are defined in half_edge.h, as well as assembling
|
|
|
|
* such a struct.
|
|
|
|
* @brief operations on half-edge data structs
|
|
|
|
*/
|
|
|
|
|
2014-05-15 16:50:57 +00:00
|
|
|
#include "common.h"
|
2014-05-10 17:33:54 +00:00
|
|
|
#include "err.h"
|
|
|
|
#include "filereader.h"
|
2014-05-10 17:16:39 +00:00
|
|
|
#include "half_edge.h"
|
2014-05-11 12:02:34 +00:00
|
|
|
#include "vector.h"
|
2014-05-10 17:16:39 +00:00
|
|
|
|
2014-05-11 12:02:34 +00:00
|
|
|
#include <stdbool.h>
|
2014-05-10 17:33:54 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2014-05-10 17:16:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
/**
|
|
|
|
* Fault intolerant macro. Will abort the program if the called
|
|
|
|
* function failed.
|
|
|
|
*/
|
|
|
|
#define GET_ALL_EMANATING_EDGES(...) \
|
|
|
|
{ \
|
|
|
|
if (!get_all_emanating_edges(__VA_ARGS__)) { \
|
|
|
|
fprintf(stderr, "Failure in get_all_emanating_edges()!\n"); \
|
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
/*
|
|
|
|
* static declarations
|
|
|
|
*/
|
2014-05-13 11:47:13 +00:00
|
|
|
static bool get_all_emanating_edges(HE_vert const * const vert,
|
|
|
|
HE_edge ***edge_array_out,
|
2014-05-12 18:20:03 +00:00
|
|
|
uint32_t *ec_out);
|
2014-05-16 18:10:43 +00:00
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-10 17:16:39 +00:00
|
|
|
/**
|
2014-05-12 18:20:03 +00:00
|
|
|
* Get all edges that emanate from vertice and return a pointer
|
|
|
|
* to that array with the size of ec_out.
|
2014-05-10 17:16:39 +00:00
|
|
|
*
|
2014-05-12 19:00:31 +00:00
|
|
|
* @param vert the vertice to get the emanating edges of
|
2014-05-13 11:47:13 +00:00
|
|
|
* @param edge_array_out address of the 2d edge array to save
|
|
|
|
* the result in [out]
|
2014-05-12 19:00:31 +00:00
|
|
|
* @param ec_out the edge counter is saved here [out]
|
2014-05-15 16:34:07 +00:00
|
|
|
* @return true/false for success/failure
|
2014-05-10 17:16:39 +00:00
|
|
|
*/
|
2014-05-13 11:47:13 +00:00
|
|
|
static bool get_all_emanating_edges(HE_vert const * const vert,
|
|
|
|
HE_edge ***edge_array_out,
|
2014-05-12 18:20:03 +00:00
|
|
|
uint32_t *ec_out)
|
2014-05-10 17:16:39 +00:00
|
|
|
{
|
2014-05-15 17:04:51 +00:00
|
|
|
uint32_t ec = 0; /* edge count */
|
2014-05-17 12:55:46 +00:00
|
|
|
uint32_t max_edges = 500; /* good guess to avoid infinite loop */
|
2014-05-15 17:04:51 +00:00
|
|
|
HE_edge **edge_array = NULL;
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-13 12:05:02 +00:00
|
|
|
if (!edge_array_out || !vert || !ec_out)
|
2014-05-13 11:47:13 +00:00
|
|
|
return false;
|
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
HE_edge *edge = vert->edge;
|
|
|
|
|
|
|
|
/* build an array of emanating edges */
|
|
|
|
do {
|
2014-05-15 17:04:51 +00:00
|
|
|
REALLOC(edge_array,
|
|
|
|
sizeof(HE_edge*) * (ec + 1));
|
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
edge_array[ec] = edge;
|
|
|
|
|
|
|
|
edge = edge->pair->next;
|
|
|
|
ec++;
|
|
|
|
|
2014-05-17 12:55:46 +00:00
|
|
|
/* sanity check */
|
|
|
|
if (ec > max_edges)
|
|
|
|
goto loop_fail_cleanup;
|
2014-05-15 16:52:28 +00:00
|
|
|
} while (edge && edge != vert->edge);
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
/* set out-pointers */
|
|
|
|
*edge_array_out = edge_array;
|
|
|
|
*ec_out = ec; /* this is the real size, not the x[ec] value */
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
return true;
|
2014-05-17 12:55:46 +00:00
|
|
|
|
|
|
|
loop_fail_cleanup:
|
|
|
|
free(edge_array);
|
|
|
|
return false;
|
2014-05-12 18:20:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 17:57:43 +00:00
|
|
|
|
2014-05-12 21:02:10 +00:00
|
|
|
/**
|
|
|
|
* Calculate the normal of a face that corresponds
|
|
|
|
* to edge.
|
|
|
|
*
|
|
|
|
* @param edge to align the normalization
|
|
|
|
* @param vec the vector to store the result in [out]
|
|
|
|
* @return true/false for success/failure
|
|
|
|
*/
|
|
|
|
bool face_normal(HE_edge const * const edge,
|
|
|
|
vector *vec)
|
|
|
|
{
|
|
|
|
vector he_vec1,
|
|
|
|
he_vec2,
|
|
|
|
he_base;
|
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
if (!edge || !vec)
|
2014-05-12 21:02:10 +00:00
|
|
|
return false;
|
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
COPY_VECTOR(edge->next->vert->vec, &he_base);
|
2014-05-12 21:02:10 +00:00
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
/* calculate vectors between the vertices */
|
|
|
|
SUB_VECTORS(edge->next->next->vert->vec, &he_base, &he_vec1);
|
|
|
|
SUB_VECTORS(edge->vert->vec, &he_base, &he_vec2);
|
2014-05-12 21:02:10 +00:00
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
VECTOR_PRODUCT(&he_vec1, &he_vec2, vec);
|
|
|
|
|
|
|
|
NORMALIZE_VECTOR(vec, vec);
|
2014-05-12 21:02:10 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
/**
|
|
|
|
* Calculate the approximated normal of a vertex.
|
|
|
|
*
|
|
|
|
* @param vert the vertex
|
|
|
|
* @param vec the vector to store the result in [out]
|
|
|
|
* @return true/false for success/failure
|
|
|
|
*/
|
|
|
|
bool vec_normal(HE_vert const * const vert, vector *vec)
|
|
|
|
{
|
2014-05-13 11:47:13 +00:00
|
|
|
HE_edge **edge_array = NULL;
|
2014-05-12 21:02:10 +00:00
|
|
|
uint32_t ec;
|
2014-05-12 18:20:03 +00:00
|
|
|
vector he_base;
|
|
|
|
|
|
|
|
if (!vert || !vec)
|
|
|
|
return false;
|
|
|
|
|
2014-05-17 12:55:46 +00:00
|
|
|
/* fault tolerance if we didn't get any
|
|
|
|
* normal */
|
|
|
|
if (!get_all_emanating_edges(vert, &edge_array, &ec))
|
|
|
|
return false;
|
|
|
|
|
2014-05-13 11:47:13 +00:00
|
|
|
COPY_VECTOR(edge_array[0]->vert->vec, &he_base);
|
|
|
|
SET_NULL_VECTOR(vec); /* set to null for later summation */
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-12 21:02:10 +00:00
|
|
|
/* iterate over all edges, get the normalized
|
|
|
|
* face vector and add those up */
|
2014-05-12 18:20:03 +00:00
|
|
|
for (uint32_t i = 0; i < ec; i++) {
|
2014-05-12 21:02:10 +00:00
|
|
|
vector new_vec;
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-16 17:04:38 +00:00
|
|
|
if (edge_array[i]->face) {
|
|
|
|
FACE_NORMAL(edge_array[i], &new_vec);
|
|
|
|
ADD_VECTORS(vec, &new_vec, vec);
|
|
|
|
}
|
2014-05-12 18:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* normalize the result */
|
2014-05-13 11:47:13 +00:00
|
|
|
NORMALIZE_VECTOR(vec, vec);
|
2014-05-12 18:20:03 +00:00
|
|
|
|
2014-05-12 21:02:10 +00:00
|
|
|
free(edge_array);
|
2014-05-12 18:20:03 +00:00
|
|
|
return true;
|
2014-05-10 17:16:39 +00:00
|
|
|
}
|
2014-05-10 17:33:54 +00:00
|
|
|
|
2014-05-10 19:05:08 +00:00
|
|
|
/**
|
|
|
|
* Find the center of an object and store the coordinates
|
|
|
|
* in a HE_vert struct.
|
|
|
|
*
|
|
|
|
* @param obj the object we want to find the center of
|
2014-05-11 12:02:34 +00:00
|
|
|
* @param vec the vector to store the result in [out]
|
|
|
|
* @return true/false for success/failure
|
2014-05-10 19:05:08 +00:00
|
|
|
*/
|
2014-05-11 12:02:34 +00:00
|
|
|
bool find_center(HE_obj const * const obj, vector *vec)
|
2014-05-10 19:05:08 +00:00
|
|
|
{
|
|
|
|
float x = 0,
|
|
|
|
y = 0,
|
|
|
|
z = 0;
|
|
|
|
uint32_t i;
|
2014-05-10 20:48:50 +00:00
|
|
|
|
2014-05-11 12:02:34 +00:00
|
|
|
if (!obj || !vec)
|
|
|
|
return false;
|
2014-05-10 20:48:50 +00:00
|
|
|
|
2014-05-10 19:05:08 +00:00
|
|
|
|
|
|
|
for (i = 0; i < obj->vc; i++) {
|
2014-05-12 17:48:46 +00:00
|
|
|
x += obj->vertices[i].vec->x;
|
|
|
|
y += obj->vertices[i].vec->y;
|
|
|
|
z += obj->vertices[i].vec->z;
|
2014-05-10 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-11 12:02:34 +00:00
|
|
|
vec->x = x / i;
|
|
|
|
vec->y = y / i;
|
|
|
|
vec->z = z / i;
|
2014-05-10 19:05:08 +00:00
|
|
|
|
2014-05-11 12:02:34 +00:00
|
|
|
return true;
|
2014-05-10 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-10 19:06:36 +00:00
|
|
|
* Calculates the factor that can be used to scale down the object
|
2014-05-10 19:05:08 +00:00
|
|
|
* to the size of 1.
|
|
|
|
*
|
|
|
|
* @param obj the object we want to scale
|
2014-05-10 20:58:51 +00:00
|
|
|
* @return the corresponding scale factor, -1 on error
|
2014-05-10 19:05:08 +00:00
|
|
|
*/
|
|
|
|
float get_normalized_scale_factor(HE_obj const * const obj)
|
|
|
|
{
|
2014-05-10 20:58:51 +00:00
|
|
|
float max;
|
|
|
|
float min;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
return -1;
|
|
|
|
|
2014-05-12 17:48:46 +00:00
|
|
|
max = obj->vertices[0].vec->x +
|
|
|
|
obj->vertices[0].vec->y + obj->vertices[0].vec->z;
|
|
|
|
min = obj->vertices[0].vec->x +
|
|
|
|
obj->vertices[0].vec->y + obj->vertices[0].vec->z;
|
2014-05-10 19:05:08 +00:00
|
|
|
|
|
|
|
for (i = 0; i < obj->vc; i++) {
|
2014-05-12 17:48:46 +00:00
|
|
|
if ((obj->vertices[i].vec->x +
|
|
|
|
obj->vertices[i].vec->y +
|
|
|
|
obj->vertices[i].vec->z) > max)
|
|
|
|
max = obj->vertices[i].vec->x +
|
|
|
|
obj->vertices[i].vec->y +
|
|
|
|
obj->vertices[i].vec->z;
|
|
|
|
else if ((obj->vertices[i].vec->x +
|
|
|
|
obj->vertices[i].vec->y +
|
|
|
|
obj->vertices[i].vec->z) < min)
|
|
|
|
min = obj->vertices[i].vec->x +
|
|
|
|
obj->vertices[i].vec->y +
|
|
|
|
obj->vertices[i].vec->z;
|
2014-05-10 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1 / (max - min);
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
/**
|
|
|
|
* Scales down the object to the size of 1. The parameter
|
|
|
|
* is modified!
|
|
|
|
*
|
|
|
|
* @param obj the object we want to scale [mod]
|
2014-05-19 16:13:51 +00:00
|
|
|
* @return true/false for success/failure
|
2014-05-12 18:20:03 +00:00
|
|
|
*/
|
2014-05-13 12:05:02 +00:00
|
|
|
bool normalize_object(HE_obj *obj)
|
2014-05-12 18:20:03 +00:00
|
|
|
{
|
|
|
|
float scale_factor;
|
2014-05-13 12:05:02 +00:00
|
|
|
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
2014-05-12 18:20:03 +00:00
|
|
|
scale_factor = get_normalized_scale_factor(obj);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
|
|
|
obj->vertices[i].vec->x = obj->vertices[i].vec->x * scale_factor;
|
|
|
|
obj->vertices[i].vec->y = obj->vertices[i].vec->y * scale_factor;
|
|
|
|
obj->vertices[i].vec->z = obj->vertices[i].vec->z * scale_factor;
|
|
|
|
}
|
2014-05-13 12:05:02 +00:00
|
|
|
|
|
|
|
return true;
|
2014-05-12 18:20:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:02:10 +00:00
|
|
|
/**
|
|
|
|
* Free the inner structures of an object.
|
|
|
|
*
|
|
|
|
* @param obj the object to free
|
|
|
|
*/
|
|
|
|
void delete_object(HE_obj *obj)
|
|
|
|
{
|
2014-05-13 12:05:02 +00:00
|
|
|
if (!obj)
|
|
|
|
return;
|
|
|
|
|
2014-05-13 19:43:56 +00:00
|
|
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
2014-05-12 21:02:10 +00:00
|
|
|
free(obj->vertices[i].vec);
|
2014-05-15 16:33:43 +00:00
|
|
|
free(obj->vertices[i].col);
|
2014-05-13 19:43:56 +00:00
|
|
|
}
|
2014-05-31 15:32:40 +00:00
|
|
|
for (uint32_t i = 0; i < obj->bzc; i++)
|
|
|
|
free(obj->bez_curves[i].vec);
|
|
|
|
|
2014-05-12 21:02:10 +00:00
|
|
|
free(obj->edges);
|
|
|
|
free(obj->vertices);
|
|
|
|
free(obj->faces);
|
2014-05-31 15:32:40 +00:00
|
|
|
free(obj->bez_curves);
|
2014-05-12 21:02:10 +00:00
|
|
|
}
|