Skip to content

Commit

Permalink
synched geometry and shape
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 11, 2024
1 parent 595554a commit 0c3e62c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
51 changes: 45 additions & 6 deletions libs/yocto/yocto_geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ struct bbox2f {
vec2f min = {flt_max, flt_max};
vec2f max = {flt_min, flt_min};

constexpr bbox2f() : min{flt_max, flt_max}, max{flt_min, flt_min} {}
constexpr bbox2f(const vec2f& min, const vec2f& max) : min{min}, max{max} {}

inline vec2f& operator[](int i);
inline const vec2f& operator[](int i) const;
};
Expand All @@ -78,6 +81,10 @@ struct bbox3f {
vec3f min = {flt_max, flt_max, flt_max};
vec3f max = {flt_min, flt_min, flt_min};

constexpr bbox3f()
: min{flt_max, flt_max, flt_max}, max{flt_min, flt_min, flt_min} {}
constexpr bbox3f(const vec3f& min, const vec3f& max) : min{min}, max{max} {}

inline vec3f& operator[](int i);
inline const vec3f& operator[](int i) const;
};
Expand All @@ -90,6 +97,10 @@ constexpr auto invalidb3f = bbox3f{};
inline vec2f center(const bbox2f& a);
inline vec2f size(const bbox2f& a);

// Bounding box tests
inline bool contains(const bbox2f& a, const vec2f& b);
inline bool contains(const bbox2f& a, const bbox2f& b);

// Bounding box comparisons.
inline bool operator==(const bbox2f& a, const bbox2f& b);
inline bool operator!=(const bbox2f& a, const bbox2f& b);
Expand All @@ -104,6 +115,10 @@ inline void expand(bbox2f& a, const bbox2f& b);
inline vec3f center(const bbox3f& a);
inline vec3f size(const bbox3f& a);

// Bounding box tests
inline bool contains(const bbox3f& a, const vec3f& b);
inline bool contains(const bbox3f& a, const bbox3f& b);

// Bounding box comparisons.
inline bool operator==(const bbox3f& a, const bbox3f& b);
inline bool operator!=(const bbox3f& a, const bbox3f& b);
Expand All @@ -129,6 +144,11 @@ struct ray2f {
vec2f d = {0, 1};
float tmin = ray_eps;
float tmax = flt_max;

constexpr ray2f() : o{0, 0}, d{0, 1}, tmin{ray_eps}, tmax{flt_max} {}
constexpr ray2f(const vec2f& o_, const vec2f& d_, float tmin_ = ray_eps,
float tmax_ = flt_max)
: o{o_}, d{d_}, tmin{tmin_}, tmax{tmax_} {}
};

// Rays with origin, direction and min/max t value.
Expand All @@ -137,6 +157,11 @@ struct ray3f {
vec3f d = {0, 0, 1};
float tmin = ray_eps;
float tmax = flt_max;

constexpr ray3f() : o{0, 0, 0}, d{0, 0, 1}, tmin{ray_eps}, tmax{flt_max} {}
constexpr ray3f(const vec3f& o_, const vec3f& d_, float tmin_ = ray_eps,
float tmax_ = flt_max)
: o{o_}, d{d_}, tmin{tmin_}, tmax{tmax_} {}
};

// Computes a point on a ray
Expand Down Expand Up @@ -365,17 +390,25 @@ inline bool overlap_bbox(const bbox3f& bbox1, const bbox3f& bbox2);
namespace yocto {

// Axis aligned bounding box represented as a min/max vector pairs.
inline vec2f& bbox2f::operator[](int i) { return (&min)[i]; }
inline vec2f& bbox2f::operator[](int i) { return (&min)[i]; }
inline const vec2f& bbox2f::operator[](int i) const { return (&min)[i]; }

// Axis aligned bounding box represented as a min/max vector pairs.
inline vec3f& bbox3f::operator[](int i) { return (&min)[i]; }
inline vec3f& bbox3f::operator[](int i) { return (&min)[i]; }
inline const vec3f& bbox3f::operator[](int i) const { return (&min)[i]; }

// Bounding box properties
inline vec2f center(const bbox2f& a) { return (a.min + a.max) / 2; }
inline vec2f size(const bbox2f& a) { return a.max - a.min; }

// Bounding box tests
inline bool contains(const bbox2f& a, const vec2f& b) {
return b.x >= a.min.x && b.x <= a.max.x && b.y >= a.min.y && b.y <= a.max.y;
}
inline bool contains(const bbox2f& a, const bbox2f& b) {
return contains(a, b.min) && contains(a, b.max);
}

// Bounding box comparisons.
inline bool operator==(const bbox2f& a, const bbox2f& b) {
return a.min == b.min && a.max == b.max;
Expand All @@ -398,6 +431,15 @@ inline void expand(bbox2f& a, const bbox2f& b) { a = merge(a, b); }
inline vec3f center(const bbox3f& a) { return (a.min + a.max) / 2; }
inline vec3f size(const bbox3f& a) { return a.max - a.min; }

// Bounding box tests
inline bool contains(const bbox3f& a, const vec3f& b) {
return b.x >= a.min.x && b.x <= a.max.x && b.y >= a.min.y && b.y <= a.max.y &&
b.z >= a.min.z && b.z <= a.max.z;
}
inline bool contains(const bbox3f& a, const bbox3f& b) {
return contains(a, b.min) && contains(a, b.max);
}

// Bounding box comparisons.
inline bool operator==(const bbox3f& a, const bbox3f& b) {
return a.min == b.min && a.max == b.max;
Expand Down Expand Up @@ -771,11 +813,8 @@ inline prim_intersection intersect_sphere(
// compute ray parameter
auto t = (-b - sqrt(dis)) / (2 * a);

// exit if not within bounds
if (t < ray.tmin || t > ray.tmax) return {};

// try other ray parameter
t = (-b + sqrt(dis)) / (2 * a);
if (t < ray.tmin || t > ray.tmax) t = (-b + sqrt(dis)) / (2 * a);

// exit if not within bounds
if (t < ray.tmin || t > ray.tmax) return {};
Expand Down
45 changes: 42 additions & 3 deletions libs/yocto/yocto_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,46 @@ fvshape_data subdivide_fvshape(
return subdivided;
}

// Transform shape
fvshape_data transform_fvshape(
const fvshape_data& shape, const frame3f& frame, bool non_rigid) {
auto transformed = shape;
for (auto& position : transformed.positions)
position = transform_point(frame, position);
for (auto& normal : transformed.normals)
normal = transform_normal(frame, normal, non_rigid);
return transformed;
}
fvshape_data scale_fvshape(
const fvshape_data& shape, float scale, float uvscale) {
if (scale == 1 && uvscale == 1) return shape;
auto transformed = shape;
for (auto& position : transformed.positions) position *= scale;
for (auto& texcoord : transformed.texcoords) texcoord *= uvscale;
return transformed;
}
fvshape_data scale_fvshape(fvshape_data&& shape, float scale, float uvscale) {
if (scale == 1 && uvscale == 1) return std::move(shape);
auto transformed = std::move(shape);
for (auto& position : transformed.positions) position *= scale;
for (auto& texcoord : transformed.texcoords) texcoord *= uvscale;
return transformed;
}

// Vertex properties
fvshape_data remove_normals(const fvshape_data& shape) {
auto transformed = shape;
transformed.quadsnorm = {};
transformed.normals = {};
return transformed;
}
fvshape_data add_normals(const fvshape_data& shape) {
auto transformed = shape;
transformed.quadsnorm = transformed.quadspos;
transformed.normals = compute_normals(shape);
return transformed;
}

vector<string> fvshape_stats(const fvshape_data& shape, bool verbose) {
auto format = [](auto num) {
auto str = std::to_string(num);
Expand Down Expand Up @@ -1500,7 +1540,6 @@ shape_data make_hair(const shape_data& base, const vec2i& steps,
return shape;
}


// Grow points around a shape
shape_data make_random_points(
const shape_data& shape, int num, float radius, uint64_t seed) {
Expand Down Expand Up @@ -4051,8 +4090,8 @@ void make_monkey(vector<vec4i>& quads, vector<vec3f>& positions,
}

void make_quad(vector<vec4i>& quads, vector<vec3f>& positions,
vector<vec3f>& normals, vector<vec2f>& texcoords, float scale,
int subdivisions) {
vector<vec3f>& normals, vector<vec2f>& texcoords, int subdivisions,
float scale) {
static const auto quad_positions = vector<vec3f>{
{-1, -1, 0}, {+1, -1, 0}, {+1, +1, 0}, {-1, +1, 0}};
static const auto quad_normals = vector<vec3f>{
Expand Down

0 comments on commit 0c3e62c

Please sign in to comment.