overte-AleziaKurdis/libraries/shared/src/AACube.h
Dale Glass 201c531edb Split SerDes into DataSerializer and DataDeserializer
Single class wasn't working well because deserialization may
need to be done on const data. With the split, the deserializer
part can work with const data without issues.

Also cleaned things up a bit.
2025-01-11 18:41:51 -08:00

119 lines
4.3 KiB
C++

//
// AACube.h
// libraries/shared/src
//
// Created by Brad Hefta-Gaub on 04/11/13.
// Copyright 2013 High Fidelity, Inc.
//
// Originally from lighthouse3d. Modified to utilize glm::vec3 and clean up to our coding standards
// Simple axis aligned box class.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_AACube_h
#define hifi_AACube_h
#include <glm/glm.hpp>
#include <QDebug>
#include "BoxBase.h"
#include "SerDes.h"
class AABox;
class Extents;
class AACube {
public:
AACube(const AABox& other);
AACube(const Extents& other);
AACube(const glm::vec3& corner, float size);
AACube();
~AACube() {};
void setBox(const glm::vec3& corner, float scale);
glm::vec3 getFarthestVertex(const glm::vec3& normal) const; // return vertex most parallel to normal
glm::vec3 getNearestVertex(const glm::vec3& normal) const; // return vertex most anti-parallel to normal
void scale(float scale);
const glm::vec3& getCorner() const { return _corner; }
float getScale() const { return _scale; }
glm::vec3 getDimensions() const { return glm::vec3(_scale,_scale,_scale); }
float getLargestDimension() const { return _scale; }
glm::vec3 calcCenter() const;
glm::vec3 calcTopFarLeft() const;
glm::vec3 getVertex(BoxVertex vertex) const;
const glm::vec3& getMinimumPoint() const { return _corner; }
glm::vec3 getMaximumPoint() const { return calcTopFarLeft(); }
bool contains(const glm::vec3& point) const;
bool contains(const AACube& otherCube) const;
bool touches(const AACube& otherCube) const;
bool contains(const AABox& otherBox) const;
bool touches(const AABox& otherBox) const;
bool expandedContains(const glm::vec3& point, float expansion) const;
bool expandedIntersectsSegment(const glm::vec3& start, const glm::vec3& end, float expansion) const;
bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& invDirection,
float& distance, BoxFace& face, glm::vec3& surfaceNormal) const;
bool findParabolaIntersection(const glm::vec3& origin, const glm::vec3& velocity, const glm::vec3& acceleration,
float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal) const;
bool touchesSphere(const glm::vec3& center, float radius) const;
bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration) const;
bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) const;
AABox clamp(const glm::vec3& min, const glm::vec3& max) const;
AABox clamp(float min, float max) const;
AACube& operator += (const glm::vec3& point);
bool containsNaN() const;
private:
glm::vec3 getClosestPointOnFace(const glm::vec3& point, BoxFace face) const;
glm::vec3 getClosestPointOnFace(const glm::vec4& origin, const glm::vec4& direction, BoxFace face) const;
glm::vec4 getPlane(BoxFace face) const;
static BoxFace getOppositeFace(BoxFace face);
glm::vec3 _corner;
float _scale;
friend DataSerializer& operator<<(DataSerializer &ser, const AACube &cube);
friend DataDeserializer& operator>>(DataDeserializer &des, AACube &cube);
};
inline bool operator==(const AACube& a, const AACube& b) {
return a.getCorner() == b.getCorner() && a.getScale() == b.getScale();
}
inline bool operator!=(const AACube& a, const AACube& b) {
return a.getCorner() != b.getCorner() || a.getScale() != b.getScale();
}
inline QDebug operator<<(QDebug debug, const AACube& cube) {
debug << "AACube[ ("
<< cube.getCorner().x << "," << cube.getCorner().y << "," << cube.getCorner().z << " ) to ("
<< cube.calcTopFarLeft().x << "," << cube.calcTopFarLeft().y << "," << cube.calcTopFarLeft().z << ") size: ("
<< cube.getDimensions().x << "," << cube.getDimensions().y << "," << cube.getDimensions().z << ")"
<< "]";
return debug;
}
inline DataSerializer& operator<<(DataSerializer &ser, const AACube &cube) {
ser << cube._corner;
ser << cube._scale;
return ser;
}
inline DataDeserializer& operator>>(DataDeserializer &des, AACube &cube) {
des >> cube._corner;
des >> cube._scale;
return des;
}
#endif // hifi_AACube_h