From 06fbd49ffabb4b0954e57b3dfc0a7ee658c872ef Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 25 Mar 2014 15:29:22 -0700 Subject: [PATCH] Adding StreamUtils for common debug output --- libraries/shared/src/StreamUtils.cpp | 75 ++++++++++++++++++++++++ libraries/shared/src/StreamUtils.h | 39 ++++++++++++ tests/physics/src/CollisionInfoTests.cpp | 7 ++- tests/physics/src/ShapeColliderTests.cpp | 5 +- 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 libraries/shared/src/StreamUtils.cpp create mode 100644 libraries/shared/src/StreamUtils.h diff --git a/libraries/shared/src/StreamUtils.cpp b/libraries/shared/src/StreamUtils.cpp new file mode 100644 index 0000000000..44c81f9464 --- /dev/null +++ b/libraries/shared/src/StreamUtils.cpp @@ -0,0 +1,75 @@ +// +// StreamUtils.cpp +// +// Created by Andrew Meadows on 2014.02.21 +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +#include + +#include "StreamUtils.h" + +const char* hex_digits = "0123456789abcdef"; + +void StreamUtil::dump(std::ostream& s, const QByteArray& buffer) { + int row_size = 32; + int i = 0; + while (i < buffer.size()) { + for(int j = 0; i < buffer.size() && j < row_size; ++j) { + char byte = buffer[i]; + s << hex_digits[(byte >> 4) & 0x0f] << hex_digits[byte & 0x0f] << " "; + ++i; + } + s << "\n"; + } +} + +std::ostream& operator<<(std::ostream& s, const glm::vec3& v) { + s << "<" << v.x << " " << v.y << " " << v.z << ">"; + return s; +} + +std::ostream& operator<<(std::ostream& s, const glm::quat& q) { + s << "<" << q.x << " " << q.y << " " << q.z << " " << q.w << ">"; + return s; +} + +std::ostream& operator<<(std::ostream& s, const glm::mat4& m) { + s << "["; + for (int j = 0; j < 4; ++j) { + s << " " << m[0][j] << " " << m[1][j] << " " << m[2][j] << " " << m[3][j] << ";"; + } + s << " ]"; + return s; +} + +// less common utils can be enabled with DEBUG +#ifdef DEBUG + +std::ostream& operator<<(std::ostream& s, const CollisionInfo& c) { + s << "{penetration=" << c._penetration + << ", contactPoint=" << c._contactPoint + << ", addedVelocity=" << c._addedVelocity + << "}"; + return s; +} + +std::ostream& operator<<(std::ostream& s, const SphereShape& sphere) { + s << "{type='sphere', center=" << sphere.getPosition() + << ", radius=" << sphere.getRadius() + << "}"; + return s; +} + +std::ostream& operator<<(std::ostream& s, const CapsuleShape& capsule) { + s << "{type='capsule', center=" << capsule.getPosition() + << ", radius=" << capsule.getRadius() + << ", length=" << (2.f * capsule.getHalfHeight()) + << ", begin=" << capsule.getStartPoint() + << ", end=" << capsule.getEndPoint() + << "}"; + return s; +} + +#endif // DEBUG + diff --git a/libraries/shared/src/StreamUtils.h b/libraries/shared/src/StreamUtils.h new file mode 100644 index 0000000000..1a17a94658 --- /dev/null +++ b/libraries/shared/src/StreamUtils.h @@ -0,0 +1,39 @@ +// +// StreamUtils.h +// +// Created by Andrew Meadows on 2014.02.21 +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +#ifndef __tests__StreamUtils__ +#define __tests__StreamUtils__ + +#include + +#include + +#include +#include + + +namespace StreamUtil { + // dump the buffer, 32 bytes per row, each byte in hex, separated by whitespace + void dump(std::ostream& s, const QByteArray& buffer); +} + +std::ostream& operator<<(std::ostream& s, const glm::vec3& v); +std::ostream& operator<<(std::ostream& s, const glm::quat& q); +std::ostream& operator<<(std::ostream& s, const glm::mat4& m); + +// less common utils can be enabled with DEBUG +#ifdef DEBUG +#include "CollisionInfo.h" +#include "SphereShape.h" +#include "CapsuleShape.h" +std::ostream& operator<<(std::ostream& s, const CollisionInfo& c); +std::ostream& operator<<(std::ostream& s, const SphereShape& shape); +std::ostream& operator<<(std::ostream& s, const CapsuleShape& capsule); +#endif // DEBUG + + +#endif // __tests__StreamUtils__ diff --git a/tests/physics/src/CollisionInfoTests.cpp b/tests/physics/src/CollisionInfoTests.cpp index 43601978ae..241204d036 100644 --- a/tests/physics/src/CollisionInfoTests.cpp +++ b/tests/physics/src/CollisionInfoTests.cpp @@ -13,12 +13,17 @@ #include #include +#include #include "CollisionInfoTests.h" -#include "PhysicsTestUtil.h" /* + +static glm::vec3 xAxis(1.f, 0.f, 0.f); +static glm::vec3 xZxis(0.f, 1.f, 0.f); +static glm::vec3 xYxis(0.f, 0.f, 1.f); + void CollisionInfoTests::rotateThenTranslate() { CollisionInfo collision; collision._penetration = xAxis; diff --git a/tests/physics/src/ShapeColliderTests.cpp b/tests/physics/src/ShapeColliderTests.cpp index 6d7e9a6db1..4a2e648323 100644 --- a/tests/physics/src/ShapeColliderTests.cpp +++ b/tests/physics/src/ShapeColliderTests.cpp @@ -16,11 +16,14 @@ #include #include #include +#include -#include "PhysicsTestUtil.h" #include "ShapeColliderTests.h" const glm::vec3 origin(0.f); +static const glm::vec3 xAxis(1.f, 0.f, 0.f); +static const glm::vec3 yAxis(0.f, 1.f, 0.f); +static const glm::vec3 zAxis(0.f, 0.f, 1.f); void ShapeColliderTests::sphereMissesSphere() { // non-overlapping spheres of unequal size