Skip to content

Commit

Permalink
join rs2::float3 with rsutils
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Mar 5, 2024
1 parent 0aa7707 commit b93c13e
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 72 deletions.
19 changes: 3 additions & 16 deletions common/float2.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

// Copyright(c) 2024 Intel Corporation. All Rights Reserved.
#pragma once

#include <cmath> // sqrt, sqrtf
#include <rsutils/number/float3.h>


namespace rs2 {


struct float2
{
float x, y;

float length() const { return sqrt( x * x + y * y ); }

float2 normalize() const { return { x / length(), y / length() }; }
};

inline float dot( const float2 & a, const float2 & b )
{
return a.x * b.x + a.y * b.y;
}
using rsutils::number::float2;


} // namespace rs2
43 changes: 3 additions & 40 deletions common/float3.h
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

// Copyright(c) 2024 Intel Corporation. All Rights Reserved.
#pragma once

#include <cmath> // sqrt, sqrtf
#include <rsutils/number/float3.h>


namespace rs2 {


struct float3
{
float x, y, z;

float length() const { return sqrt( x * x + y * y + z * z ); }

float3 normalize() const { return ( length() > 0 ) ? float3{ x / length(), y / length(), z / length() } : *this; }
};

inline float3 cross( const float3 & a, const float3 & b )
{
return { a.y * b.z - b.y * a.z, a.x * b.z - b.x * a.z, a.x * b.y - a.y * b.x };
}

inline float operator*(const float3& a, const float3& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}

inline float3 operator*( const float3 & a, float t )
{
return { a.x * t, a.y * t, a.z * t };
}

inline float3 operator/( const float3 & a, float t )
{
return { a.x / t, a.y / t, a.z / t };
}

inline float3 operator+( const float3 & a, const float3 & b )
{
return { a.x + b.x, a.y + b.y, a.z + b.z };
}

inline float3 operator-( const float3 & a, const float3 & b )
{
return { a.x - b.x, a.y - b.y, a.z - b.z };
}
using rsutils::number::float3;


} // namespace rs2
6 changes: 3 additions & 3 deletions common/measurement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ float measurement::calculate_area(std::vector<float3> poly)
auto a = poly[1] - poly[0];
auto b = poly[2] - poly[0];
auto n = cross(a, b);
return std::abs( total * n.normalize() ) / 2;
return std::abs( total * n.normalized() ) / 2;
}

void draw_sphere(const float3& pos, float r, int lats, int longs)
Expand Down Expand Up @@ -461,10 +461,10 @@ void measurement::draw(ux_window& win)

auto axis1 = cross(vec3d{ _normal.x, _normal.y, _normal.z }, vec3d{ 0.f, 1.f, 0.f });
auto faxis1 = float3 { axis1.x, axis1.y, axis1.z };
faxis1.normalize();
faxis1.normalized();
auto axis2 = cross(vec3d{ _normal.x, _normal.y, _normal.z }, axis1);
auto faxis2 = float3 { axis2.x, axis2.y, axis2.z };
faxis2.normalize();
faxis2.normalized();

matrix4 basis = matrix4::identity();
basis(0, 0) = faxis1.x;
Expand Down
7 changes: 1 addition & 6 deletions common/opengl3.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020 Intel Corporation. All Rights Reserved.

// Copyright(c) 2024 Intel Corporation. All Rights Reserved.
#pragma once

#include "matrix4.h"
Expand Down Expand Up @@ -130,10 +129,6 @@ namespace rs2
vbo_type _type;
};

struct float3;
struct float2;
struct int3;

struct obj_mesh;

class vao
Expand Down
4 changes: 2 additions & 2 deletions common/rect.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

// Copyright(c) 2024 Intel Corporation. All Rights Reserved.
#pragma once

#include "float2.h"

#include <algorithm> // max,min
#include <cmath> // floor


namespace rs2 {
Expand Down
4 changes: 2 additions & 2 deletions common/rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ namespace rs2
auto p2 = p[(i+1) % p.size()];
if ((p2 - p1).length() < 1e-3) return false;

p1 = p1.normalize();
p2 = p2.normalize();
p1 = p1.normalized();
p2 = p2.normalized();

angles.push_back(acos((p1 * p2) / sqrt(p1.length() * p2.length())));
}
Expand Down
2 changes: 1 addition & 1 deletion common/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ namespace rs2
// up = cross(cross(look, world_up), look)
{
float3 look = { target.x - pos.x, target.y - pos.y, target.z - pos.z };
look = look.normalize();
look = look.normalized();

float world_up[3] = { 0.0f, 1.0f, 0.0f };

Expand Down
2 changes: 1 addition & 1 deletion src/gl/pc-shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ namespace librealsense

auto right_left = lerp(pos - left, right - pos, 0.5f);
auto down_up = lerp(pos - up, down - pos, 0.5f) * (-1.f);
*normal = cross(right_left, down_up).normalize();
*normal = cross(right_left, down_up).normalized();
*xyz = pos;
}
}
Expand Down
27 changes: 27 additions & 0 deletions third-party/rsutils/include/rsutils/number/float3.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct float2
assert( i < 2 );
return *( &x + i );
}
float length() const;
float2 normalized() const;
};
struct float3
{
Expand All @@ -39,6 +41,8 @@ struct float3
assert( i < 3 );
return ( *( &x + i ) );
}
float length() const;
float3 normalized() const;
};
struct float4
{
Expand All @@ -64,6 +68,13 @@ struct float3x3
}; // column-major
#pragma pack( pop )


inline float dot( const float2 & a, const float2 & b )
{
return a.x * b.x + a.y * b.y;
}


inline bool operator==( const float3 & a, const float3 & b )
{
return a.x == b.x && a.y == b.y && a.z == b.z;
Expand All @@ -80,6 +91,20 @@ inline float3 operator*( const float3 & a, float b )
{
return { a.x * b, a.y * b, a.z * b };
}
inline float3 operator/( const float3 & a, float t )
{
return { a.x / t, a.y / t, a.z / t };
}
inline float operator*( const float3 & a, const float3 & b )
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float3 cross( const float3 & a, const float3 & b )
{
return { a.y * b.z - b.y * a.z, a.x * b.z - b.x * a.z, a.x * b.y - a.y * b.x };
}


inline bool operator==( const float4 & a, const float4 & b )
{
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
Expand All @@ -92,6 +117,8 @@ inline float4 operator-( const float4 & a, const float4 & b )
{
return { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w };
}


inline bool operator==( const float3x3 & a, const float3x3 & b )
{
return a.x == b.x && a.y == b.y && a.z == b.z;
Expand Down
31 changes: 31 additions & 0 deletions third-party/rsutils/src/float3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <rsutils/number/float3.h>

#include <cmath> // sqrt, sqrtf
#include <ostream>


Expand All @@ -28,5 +29,35 @@ std::ostream & operator<<( std::ostream & stream, const float3x3 & elem )
}


float float2::length() const
{
return sqrt( x * x + y * y );
}


float2 float2::normalized() const
{
auto const l = length();
if( l <= 0.f )
return *this;
return { x / l, y / l };
}


float float3::length() const
{
return sqrt( x * x + y * y + z * z );
}


float3 float3::normalized() const
{
auto const l = length();
if( l <= 0.f )
return *this;
return { x / l, y / l, z / l };
}


} // namespace number
} // namespace rsutils
2 changes: 1 addition & 1 deletion tools/depth-quality/depth-metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace rs2
dir = { a, b, 1 };
}

return plane_from_point_and_normal(centroid, dir.normalize());
return plane_from_point_and_normal(centroid, dir.normalized());
}

inline double evaluate_pixel(const plane& p, const rs2_intrinsics* intrin, float x, float y, float distance, float3& output)
Expand Down

0 comments on commit b93c13e

Please sign in to comment.