123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #include "core/Box.h"
- #include "core/Generic.h"
- #include "core/ToString.h"
- void setBox(Box* box, const Vector3* size) {
- for(size_t i = 0; i < 3; i++) {
- if(size->data[i] < 0.0f) {
- box->min.data[i] = size->data[i];
- box->max.data[i] = 0.0f;
- } else {
- box->min.data[i] = 0.0f;
- box->max.data[i] = size->data[i];
- }
- }
- }
- void offsetBox(Box* box, const Vector3* offset) {
- addSet(&box->min, offset);
- addSet(&box->max, offset);
- }
- bool collidesWithBox(const Box* box, const Box* other) {
- return box->max.x > other->min.x && box->min.x < other->max.x &&
- box->max.y > other->min.y && box->min.y < other->max.y &&
- box->max.z > other->min.z && box->min.z < other->max.z;
- }
- void expandBox(Box* box, const Vector3* offset) {
- for(size_t i = 0; i < 3; i++) {
- box->v[offset->data[i] > 0.0f].data[i] += offset->data[i];
- }
- }
- void growBox(Box* box, const Vector3* growth) {
- Vector3 half;
- mul(&half, growth, 0.5f);
- Vector3 nMin;
- sub(&nMin, &box->min, &half);
- Vector3 nMax;
- add(&nMax, &box->max, &half);
- for(size_t i = 0; i < 3; i++) {
- if(nMin.data[i] > nMax.data[i]) {
- nMin.data[i] = (box->min.data[i] + box->max.data[i]) * 0.5f;
- nMax.data[i] = nMin.data[i];
- }
- }
- box->min = nMin;
- box->max = nMax;
- }
- size_t toStringBox(const Box* box, char* buffer, size_t n) {
- return toString(
- buffer, n, "Box([%.3f, %.3f, %.3f], [%.3f, %.3f, %.3f])",
- (double)box->min.x, (double)box->min.y, (double)box->min.z,
- (double)box->max.x, (double)box->max.y, (double)box->max.z);
- }
|