add usd-21-05-win64_py37
This commit is contained in:
1 parent
41d23e71ba
commit
66bc054336
15675 files changed
+3061008
No files matched your search
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_ALIGN_H
|
||||
#define PXR_BASE_ARCH_ALIGN_H
|
||||
|
||||
/// \file arch/align.h
|
||||
/// \ingroup group_arch_Memory
|
||||
/// Provide architecture-specific memory-alignment information.
|
||||
|
||||
#if !defined(__cplusplus)
|
||||
#error This include file can only be included in C++ programs.
|
||||
#endif
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_Memory
|
||||
///@{
|
||||
|
||||
/// Return suitably aligned memory size.
|
||||
///
|
||||
/// Requests to \c malloc() or \c ::new for a given size are often rounded
|
||||
/// upward. Given a request for \c nBytes bytes of storage, this function
|
||||
/// returns the amount that would actually be consumed by the system to
|
||||
/// satisfy it. This is needed for efficient user-defined memory management.
|
||||
///
|
||||
inline size_t
|
||||
ArchAlignMemorySize(size_t nBytes) {
|
||||
return (nBytes + 7) & (~0x7);
|
||||
}
|
||||
|
||||
/// Maximum extra space needed for alignment.
|
||||
///
|
||||
/// The \c ArchAlignMemorySize() can increase the required memory by no more
|
||||
/// than \c ARCH_MAX_ALIGNMENT_INCREASE.
|
||||
///
|
||||
/// \hideinitializer
|
||||
#define ARCH_MAX_ALIGNMENT_INCREASE 7
|
||||
|
||||
/// Align memory to the next "best" alignment value.
|
||||
///
|
||||
/// This will take a pointer and bump it to the next ideal alignment boundary
|
||||
/// that will work for all data types.
|
||||
///
|
||||
inline void *
|
||||
ArchAlignMemory(void *base)
|
||||
{
|
||||
return reinterpret_cast<void *>
|
||||
((reinterpret_cast<uintptr_t>(base) + 7) & ~0x7);
|
||||
}
|
||||
|
||||
/// The size of a CPU cache line on the current processor architecture in bytes.
|
||||
///
|
||||
/// \hideinitializer
|
||||
#if defined(ARCH_OS_DARWIN) && defined(ARCH_CPU_ARM)
|
||||
#define ARCH_CACHE_LINE_SIZE 128
|
||||
#else
|
||||
#define ARCH_CACHE_LINE_SIZE 64
|
||||
#endif
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_ALIGN_H
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_API_H
|
||||
#define PXR_BASE_ARCH_API_H
|
||||
|
||||
#include "pxr/base/arch/export.h"
|
||||
|
||||
#if defined(PXR_STATIC)
|
||||
# define ARCH_API
|
||||
# define ARCH_API_TEMPLATE_CLASS(...)
|
||||
# define ARCH_API_TEMPLATE_STRUCT(...)
|
||||
# define ARCH_LOCAL
|
||||
#else
|
||||
# if defined(ARCH_EXPORTS)
|
||||
# define ARCH_API ARCH_EXPORT
|
||||
# define ARCH_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define ARCH_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# else
|
||||
# define ARCH_API ARCH_IMPORT
|
||||
# define ARCH_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define ARCH_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# endif
|
||||
# define ARCH_LOCAL ARCH_HIDDEN
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,335 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_ATTRIBUTES_H
|
||||
#define PXR_BASE_ARCH_ATTRIBUTES_H
|
||||
|
||||
/// \file arch/attributes.h
|
||||
/// Define function attributes.
|
||||
///
|
||||
/// This file allows you to define architecture-specific or compiler-specific
|
||||
/// options to be used outside lib/arch.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/export.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
#if defined(doxygen)
|
||||
|
||||
/// Macro used to indicate a function takes a printf-like specification.
|
||||
///
|
||||
/// This attribute is used as follows:
|
||||
/// \code
|
||||
/// void PrintFunc(T1 arg1, T2 arg2, const char* fmt, ...)
|
||||
/// ARCH_PRINTF_FUNCTION(3, 4)
|
||||
/// \endcode
|
||||
/// This indicates that the third argument is the format string, and that the
|
||||
/// fourth argument is where the var-args corresponding to the format string begin.
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
|
||||
|
||||
/// Macro used to indicate a function takes a scanf-like specification.
|
||||
///
|
||||
/// This attribute is used as follows:
|
||||
/// \code
|
||||
/// void ScanFunc(T1 arg1, T2 arg2, const char* fmt, ...)
|
||||
/// ARCH_PRINTF_FUNCTION(3, 4)
|
||||
/// \endcode
|
||||
/// This indicates that the third argument is the format string, and
|
||||
/// that the fourth argument is where the var-args corresponding to the
|
||||
/// format string begin.
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_SCANF_FUNCTION(_fmt, _firstArg)
|
||||
|
||||
/// Macro used to indicate that a function should never be inlined.
|
||||
///
|
||||
/// This attribute is used as follows:
|
||||
/// \code
|
||||
/// void Func(T1 arg1, T2 arg2) ARCH_NOINLINE;
|
||||
/// \endcode
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_NOINLINE
|
||||
|
||||
/// Macro used to indicate a function parameter may be unused.
|
||||
///
|
||||
/// In general, avoid this attribute if possible. Mostly this attribute
|
||||
/// should be used when the set of arguments to a function is described
|
||||
/// as part of a macro. The usage is:
|
||||
/// \code
|
||||
/// void Func(T1 arg1, ARCH_UNUSED_ARG T2 arg2, ARCH_UNUSED_ARG T3 arg3, T4 arg4) {
|
||||
/// ...
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_UNUSED_ARG
|
||||
|
||||
/// Macro used to indicate a function may be unused.
|
||||
///
|
||||
/// In general, avoid this attribute if possible. Mostly this attribute
|
||||
/// should be used when you need to keep a function around (for some
|
||||
/// good reason), but it is not used in the rest of the code. The usage
|
||||
/// is:
|
||||
/// \code
|
||||
/// ARCH_UNUSED_FUNCTION void Func() {
|
||||
/// ...
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_UNUSED_FUNCTION
|
||||
|
||||
/// Macro used to indicate that a function's code must always be emitted even
|
||||
/// if not required.
|
||||
///
|
||||
/// This attribute is especially useful with templated registration functions,
|
||||
/// which might not be present in the linked binary if they are not used (or
|
||||
/// the compiler optimizes away their use.)
|
||||
///
|
||||
/// The usage is:
|
||||
/// \code
|
||||
/// template <typename T>
|
||||
/// struct TraitsClass {
|
||||
/// static void RegistryFunction() ARCH_USED_FUNCTION {
|
||||
/// ...
|
||||
/// }
|
||||
/// };
|
||||
/// \endcode
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_USED_FUNCTION
|
||||
|
||||
/// Macro to begin the definition of a function that should be executed by
|
||||
/// the dynamic loader when the dynamic object (library or program) is
|
||||
/// loaded.
|
||||
///
|
||||
/// \p _priority is used to order the execution of constructors. Valid
|
||||
/// values are integers in the range [0,255]. Constructors with lower
|
||||
/// numbers are run first. It is unspecified if these functions are run
|
||||
/// before or after dynamic initialization of non-local variables.
|
||||
///
|
||||
/// \p _name is the name of the function and must be unique across all
|
||||
/// invocations of ARCH_CONSTRUCTOR in the same translation unit.
|
||||
/// The remaining arguments should be types for the signature of the
|
||||
/// function. The types are only to make the name unique (when mangled);
|
||||
/// the function will be called with no arguments so the arguments must
|
||||
/// not be used. If you don't need any arguments you must use void.
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_CONSTRUCTOR(_name, _priority, ...)
|
||||
|
||||
/// Macro to begin the definition of a function that should be executed by
|
||||
/// the dynamic loader when the dynamic object (library or program) is
|
||||
/// unloaded.
|
||||
///
|
||||
/// \p _priority is used to order the execution of destructors. Valid
|
||||
/// values are integers in the range [0,255]. Destructors with higher
|
||||
/// numbers are run first. It is unspecified if these functions are run
|
||||
/// before or after dynamically initialized non-local variables.
|
||||
///
|
||||
/// \p _name is the name of the function and must be unique across all
|
||||
/// invocations of ARCH_CONSTRUCTOR in the same translation unit.
|
||||
/// The remaining arguments should be types for the signature of the
|
||||
/// function. The types are only to make the name unique (when mangled);
|
||||
/// the function will be called with no arguments so the arguments must
|
||||
/// not be used. If you don't need any arguments you must use void.
|
||||
///
|
||||
/// \hideinitializer
|
||||
# define ARCH_DESTRUCTOR(_name, _priority, ...)
|
||||
|
||||
#elif defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
|
||||
|
||||
# define ARCH_PRINTF_FUNCTION(_fmt, _firstArg) \
|
||||
__attribute__((format(printf, _fmt, _firstArg)))
|
||||
# define ARCH_SCANF_FUNCTION(_fmt, _firstArg) \
|
||||
__attribute__((format(scanf, _fmt, _firstArg)))
|
||||
# define ARCH_NOINLINE __attribute__((noinline))
|
||||
# define ARCH_UNUSED_ARG __attribute__ ((unused))
|
||||
# define ARCH_UNUSED_FUNCTION __attribute__((unused))
|
||||
# define ARCH_USED_FUNCTION __attribute__((used))
|
||||
|
||||
#elif defined(ARCH_COMPILER_MSVC)
|
||||
|
||||
# define ARCH_PRINTF_FUNCTION(_fmt, _firstArg)
|
||||
# define ARCH_SCANF_FUNCTION(_fmt, _firstArg)
|
||||
# define ARCH_NOINLINE // __declspec(noinline)
|
||||
# define ARCH_UNUSED_ARG
|
||||
# define ARCH_UNUSED_FUNCTION
|
||||
# define ARCH_USED_FUNCTION
|
||||
|
||||
#else
|
||||
|
||||
// Leave macros undefined so we'll fail to build on a new system/compiler
|
||||
// rather than fail mysteriously at runtime.
|
||||
|
||||
#endif
|
||||
|
||||
// Helper to do on-demand static initialziation. We need to insert per-library
|
||||
// static initializers if the ARCH_CONSTRUCTOR macros are used, etc, but we
|
||||
// don't want that to happen otherwise. This mechanism makes that possible. It
|
||||
// works by creating a class template (Arch_PerLibInit) that has hidden
|
||||
// visibility and a static member of its template parameter (StaticInit). In
|
||||
// its constructor, it "uses" its static member 'init', in order to ensure it
|
||||
// gets instantiated. Since it's a static member, it gets initialized only if
|
||||
// it's instantiated. This lets us have macros like ARCH_CONSTRUCTOR() that
|
||||
// require a static initializer, but to only emit that static initializer in
|
||||
// translation units that actually invoke the macro. Clients typically do this
|
||||
// by way of the _ARCH_ENSURE_PER_LIB_INIT macro. This is tested on all current
|
||||
// supported compilers (clang, gcc, msvc). The hidden visibility is required to
|
||||
// ensure that each library gets its own initialization. Without it, on Linux,
|
||||
// there would be exactly *one* initialization no matter how many libraries are
|
||||
// loaded.
|
||||
template <class StaticInit>
|
||||
struct ARCH_HIDDEN Arch_PerLibInit {
|
||||
Arch_PerLibInit() { /* "use" of init here forces instantiation */
|
||||
(void)init; }
|
||||
private:
|
||||
static StaticInit init;
|
||||
};
|
||||
template <class StaticInit>
|
||||
StaticInit Arch_PerLibInit<StaticInit>::init;
|
||||
|
||||
#define _ARCH_CAT_NOEXPAND(a, b) a ## b
|
||||
#define _ARCH_CAT(a, b) _ARCH_CAT_NOEXPAND(a, b)
|
||||
#define _ARCH_ENSURE_PER_LIB_INIT(T, prefix) \
|
||||
static Arch_PerLibInit<T> _ARCH_CAT(prefix, __COUNTER__)
|
||||
|
||||
#if defined(doxygen)
|
||||
|
||||
// The macros are already defined above in doxygen.
|
||||
|
||||
#elif defined(ARCH_OS_DARWIN)
|
||||
|
||||
// Entry for a constructor/destructor in the custom section.
|
||||
struct Arch_ConstructorEntry {
|
||||
typedef void (*Type)(void);
|
||||
Type function;
|
||||
unsigned int version:24; // USD version
|
||||
unsigned int priority:8; // Priority of function
|
||||
};
|
||||
|
||||
// Emit a Arch_ConstructorEntry in the __Data,pxrctor section.
|
||||
# define ARCH_CONSTRUCTOR(_name, _priority, ...) \
|
||||
static void _name(__VA_ARGS__); \
|
||||
static const Arch_ConstructorEntry _ARCH_CAT_NOEXPAND(arch_ctor_, _name) \
|
||||
__attribute__((used, section("__DATA,pxrctor"))) = { \
|
||||
reinterpret_cast<Arch_ConstructorEntry::Type>(&_name), \
|
||||
0u, \
|
||||
_priority \
|
||||
}; \
|
||||
static void _name(__VA_ARGS__)
|
||||
|
||||
// Emit a Arch_ConstructorEntry in the __Data,pxrdtor section.
|
||||
# define ARCH_DESTRUCTOR(_name, _priority, ...) \
|
||||
static void _name(__VA_ARGS__); \
|
||||
static const Arch_ConstructorEntry _ARCH_CAT_NOEXPAND(arch_dtor_, _name) \
|
||||
__attribute__((used, section("__DATA,pxrdtor"))) = { \
|
||||
reinterpret_cast<Arch_ConstructorEntry::Type>(&_name), \
|
||||
0u, \
|
||||
_priority \
|
||||
}; \
|
||||
static void _name(__VA_ARGS__)
|
||||
|
||||
#elif defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
|
||||
|
||||
// The used attribute is required to prevent these apparently unused functions
|
||||
// from being removed by the linker.
|
||||
# define ARCH_CONSTRUCTOR(_name, _priority, ...) \
|
||||
__attribute__((used, section(".pxrctor"), constructor((_priority) + 100))) \
|
||||
static void _name(__VA_ARGS__)
|
||||
# define ARCH_DESTRUCTOR(_name, _priority, ...) \
|
||||
__attribute__((used, section(".pxrdtor"), destructor((_priority) + 100))) \
|
||||
static void _name(__VA_ARGS__)
|
||||
|
||||
#elif defined(ARCH_OS_WINDOWS)
|
||||
|
||||
# include "pxr/base/arch/api.h"
|
||||
|
||||
// Entry for a constructor/destructor in the custom section.
|
||||
__declspec(align(16))
|
||||
struct Arch_ConstructorEntry {
|
||||
typedef void (__cdecl *Type)(void);
|
||||
Type function;
|
||||
unsigned int version:24; // USD version
|
||||
unsigned int priority:8; // Priority of function
|
||||
};
|
||||
|
||||
// Declare the special sections.
|
||||
# pragma section(".pxrctor", read)
|
||||
# pragma section(".pxrdtor", read)
|
||||
|
||||
// Objects of this type run the ARCH_CONSTRUCTOR and ARCH_DESTRUCTOR functions
|
||||
// for the library containing the object in the c'tor and d'tor, respectively.
|
||||
// Each HMODULE is handled at most once.
|
||||
struct Arch_ConstructorInit {
|
||||
ARCH_API Arch_ConstructorInit();
|
||||
ARCH_API ~Arch_ConstructorInit();
|
||||
};
|
||||
|
||||
// Emit a Arch_ConstructorEntry in the .pxrctor section. The namespace and
|
||||
// extern are to convince the compiler and linker to leave the object in the
|
||||
// final library/executable instead of stripping it out. In clang/gcc we use
|
||||
// __attribute__((used)) to do that.
|
||||
# define ARCH_CONSTRUCTOR(_name, _priority, ...) \
|
||||
static void _name(__VA_ARGS__); \
|
||||
namespace { \
|
||||
__declspec(allocate(".pxrctor")) \
|
||||
extern const Arch_ConstructorEntry \
|
||||
_ARCH_CAT_NOEXPAND(arch_ctor_, _name) = { \
|
||||
reinterpret_cast<Arch_ConstructorEntry::Type>(&_name), \
|
||||
0u, \
|
||||
_priority \
|
||||
}; \
|
||||
} \
|
||||
_ARCH_ENSURE_PER_LIB_INIT(Arch_ConstructorInit, _archCtorInit); \
|
||||
static void _name(__VA_ARGS__)
|
||||
|
||||
// Emit a Arch_ConstructorEntry in the .pxrdtor section.
|
||||
# define ARCH_DESTRUCTOR(_name, _priority, ...) \
|
||||
static void _name(__VA_ARGS__); \
|
||||
namespace { \
|
||||
__declspec(allocate(".pxrdtor")) \
|
||||
extern const Arch_ConstructorEntry \
|
||||
_ARCH_CAT_NOEXPAND(arch_dtor_, _name) = { \
|
||||
reinterpret_cast<Arch_ConstructorEntry::Type>(&_name), \
|
||||
0u, \
|
||||
_priority \
|
||||
}; \
|
||||
} \
|
||||
_ARCH_ENSURE_PER_LIB_INIT(Arch_ConstructorInit, _archCtorInit); \
|
||||
static void _name(__VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
// Leave macros undefined so we'll fail to build on a new system/compiler
|
||||
// rather than fail mysteriously at runtime.
|
||||
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_ATTRIBUTES_H
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_BUILD_MODE_H
|
||||
#define PXR_BASE_ARCH_BUILD_MODE_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
struct ArchBuildMode {
|
||||
// Check if the build system has specified a build mode, falling
|
||||
// back to commonly-used macros if it has not. (Typically, _DEBUG
|
||||
// is defined by Visual Studio and DEBUG by Xcode for debug-mode builds)
|
||||
#if defined(BUILD_OPTLEVEL_DEV) || defined(_DEBUG) || defined(DEBUG)
|
||||
enum { DEV_BUILD = 1 };
|
||||
#else
|
||||
enum { DEV_BUILD = 0 };
|
||||
#endif
|
||||
};
|
||||
|
||||
#define ARCH_DEV_BUILD ArchBuildMode::DEV_BUILD
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_BUILD_MODE_H
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_DAEMON_H
|
||||
#define PXR_BASE_ARCH_DAEMON_H
|
||||
|
||||
/// \file arch/daemon.h
|
||||
/// \ingroup group_arch_Multithreading
|
||||
/// Create background or daemon processes.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Close all file descriptors (with possible exceptions)
|
||||
///
|
||||
/// \c ArchCloseAllFiles will close all file descriptors open in the
|
||||
/// current process. Occasionally you'd like to close all files except
|
||||
/// for some small subset (like 0, 1, and 2). The \p nExcept and \p
|
||||
/// exceptFds arguments can be used to provide the list of exceptions.
|
||||
/// \c ArchDaemonizeProcess uses this method to close all unwanted file
|
||||
/// descriptors in the daemon process.
|
||||
///
|
||||
/// \p nExcept should be the number of elements in the \p exceptFds array.
|
||||
/// Invalid file descriptors in exceptFds are ignored.
|
||||
///
|
||||
/// \note Be \b very careful when using this routine. It is intended
|
||||
/// to be used after a \c fork(2) call to close \b all unwanted file
|
||||
/// descriptors. However, it does not flush stdio buffers, wait for
|
||||
/// processes opened with popen, shut down the X11 display connection,
|
||||
/// or anything. It just slams closed all the file descriptors. This
|
||||
/// is appropriate following a \c fork(2) call as all these file
|
||||
/// descriptors are duplicates of the ones in the parent process and
|
||||
/// shutting down the X11 display connection would mess up the parent's
|
||||
/// X11 display. But you shouldn't use \c ArchCloseAllFiles unless you
|
||||
/// know what you are doing.
|
||||
///
|
||||
/// \return -1 on error and \c errno will be set to an appropriate
|
||||
/// value. Returns 0 on success.
|
||||
///
|
||||
/// \ingroup group_arch_Multithreading
|
||||
ARCH_API
|
||||
int ArchCloseAllFiles(int nExcept, const int* exceptFds);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_DAEMON_H
|
||||
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_DEBUGGER_H
|
||||
#define PXR_BASE_ARCH_DEBUGGER_H
|
||||
|
||||
/// \file arch/debugger.h
|
||||
/// Routines for interacting with a debugger.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/attributes.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Stop in a debugger.
|
||||
///
|
||||
/// This function will do one of the following: start a debugger
|
||||
/// attached to this process stopped on this function; stop in an
|
||||
/// already attached debugger; stop and wait for a debugger to
|
||||
/// attach, or nothing.
|
||||
///
|
||||
/// On Linux this will start a debugger using \c ArchDebuggerAttach()
|
||||
/// if no debugger is attached. If a debugger is (or was) attached it
|
||||
/// will stop on this function due to \c SIGTRAP. Alternatively, users
|
||||
/// can configure the debugger to not stop on \c SIGTRAP and instead
|
||||
/// break on \c ArchDebuggerTrap().
|
||||
///
|
||||
/// If a debugger is not attached, \c ArchDebuggerAttach() does not
|
||||
/// attach one, and \c ArchDebuggerWait() has been most recently
|
||||
/// called with \c true then this will wait for a debugger to attach,
|
||||
/// otherwise it does nothing and the process does not stop. The user
|
||||
/// can continue the process from the debugger simply by issuing the
|
||||
/// continue command. The user can also continue the process from an
|
||||
/// attached terminal by putting the process into the foreground or
|
||||
/// background.
|
||||
///
|
||||
ARCH_API
|
||||
void ArchDebuggerTrap() ARCH_NOINLINE;
|
||||
|
||||
/// Cause debug traps to wait for the debugger or not.
|
||||
///
|
||||
/// When \p wait is \c true the next call to \c ArchDebuggerTrap()
|
||||
/// will cause the process to wait for a signal. The user can attach
|
||||
/// a debugger to continue the process. The process will not wait
|
||||
/// again until another call to this function with \p wait \c true.
|
||||
///
|
||||
ARCH_API
|
||||
void ArchDebuggerWait(bool wait);
|
||||
|
||||
/// Attach a debugger.
|
||||
///
|
||||
/// Attaches the debugger by running the contents of the enviroment variable
|
||||
/// ARCH_DEBUGGER using /bin/sh. Any '%p' in the contents of this variable
|
||||
/// will be replaced with the process id of the process launching the debugger.
|
||||
/// Any '%e' will be replaced with the path to the executable for the process.
|
||||
///
|
||||
/// Returns true if ARCH_DEBUGGER is set and the debugger was successfully
|
||||
/// launched, otherwise returns false.
|
||||
ARCH_API
|
||||
bool ArchDebuggerAttach() ARCH_NOINLINE;
|
||||
|
||||
/// Test if a debugger is attached
|
||||
///
|
||||
/// Attempts to detect if a debugger is currently attached to the process.
|
||||
ARCH_API
|
||||
bool ArchDebuggerIsAttached() ARCH_NOINLINE;
|
||||
|
||||
/// Abort. This will try to avoid the JIT debugger if any if ARCH_AVOID_JIT
|
||||
/// is in the environment and the debugger isn't already attached. In that
|
||||
/// case it will _exit(134). If \p logging is \c false then this will
|
||||
/// attempt to bypass any crash logging.
|
||||
[[noreturn]]
|
||||
ARCH_API
|
||||
void ArchAbort(bool logging = true);
|
||||
|
||||
/// Stop in the debugger.
|
||||
///
|
||||
/// This macro expands to \c ArchDebuggerTrap() and, if necessary and
|
||||
/// possible, code to prevent optimization so the caller appears in the
|
||||
/// debugger's stack trace. The calling functions should also use the
|
||||
/// \c ARCH_NOINLINE function attribute.
|
||||
#if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
|
||||
#define ARCH_DEBUGGER_TRAP do { ArchDebuggerTrap(); asm(""); } while (0)
|
||||
#else
|
||||
#define ARCH_DEBUGGER_TRAP do { ArchDebuggerTrap(); } while (0)
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_DEBUGGER_H
|
||||
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_DEFINES_H
|
||||
#define PXR_BASE_ARCH_DEFINES_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
//
|
||||
// OS
|
||||
//
|
||||
|
||||
#if defined(__linux__)
|
||||
#define ARCH_OS_LINUX
|
||||
#elif defined(__APPLE__)
|
||||
#include "TargetConditionals.h"
|
||||
#define ARCH_OS_DARWIN
|
||||
#if TARGET_OS_IPHONE
|
||||
#define ARCH_OS_IOS
|
||||
#else
|
||||
#define ARCH_OS_OSX
|
||||
#endif
|
||||
#elif defined(_WIN32) || defined(_WIN64)
|
||||
#define ARCH_OS_WINDOWS
|
||||
#endif
|
||||
|
||||
//
|
||||
// Processor
|
||||
//
|
||||
|
||||
#if defined(i386) || defined(__i386__) || defined(__x86_64__) || \
|
||||
defined(_M_IX86) || defined(_M_X64)
|
||||
#define ARCH_CPU_INTEL
|
||||
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM)
|
||||
#define ARCH_CPU_ARM
|
||||
#endif
|
||||
|
||||
//
|
||||
// Bits
|
||||
//
|
||||
|
||||
#if defined(__x86_64__) || defined(__aarch64__) || defined(_M_X64)
|
||||
#define ARCH_BITS_64
|
||||
#else
|
||||
#error "Unsupported architecture. x86_64 or ARM64 required."
|
||||
#endif
|
||||
|
||||
//
|
||||
// Compiler
|
||||
//
|
||||
|
||||
#if defined(__clang__)
|
||||
#define ARCH_COMPILER_CLANG
|
||||
#define ARCH_COMPILER_CLANG_MAJOR __clang_major__
|
||||
#define ARCH_COMPILER_CLANG_MINOR __clang_minor__
|
||||
#define ARCH_COMPILER_CLANG_PATCHLEVEL __clang_patchlevel__
|
||||
#elif defined(__GNUC__)
|
||||
#define ARCH_COMPILER_GCC
|
||||
#define ARCH_COMPILER_GCC_MAJOR __GNUC__
|
||||
#define ARCH_COMPILER_GCC_MINOR __GNUC_MINOR__
|
||||
#define ARCH_COMPILER_GCC_PATCHLEVEL __GNUC_PATCHLEVEL__
|
||||
#elif defined(__ICC)
|
||||
#define ARCH_COMPILER_ICC
|
||||
#elif defined(_MSC_VER)
|
||||
#define ARCH_COMPILER_MSVC
|
||||
#define ARCH_COMPILER_MSVC_VERSION _MSC_VER
|
||||
#endif
|
||||
|
||||
//
|
||||
// Features
|
||||
//
|
||||
|
||||
// Only use the GNU STL extensions on Linux when using gcc.
|
||||
#if defined(ARCH_OS_LINUX) && defined(ARCH_COMPILER_GCC)
|
||||
#define ARCH_HAS_GNU_STL_EXTENSIONS
|
||||
#endif
|
||||
|
||||
// The current version of Apple clang does not support the thread_local
|
||||
// keyword.
|
||||
#if !(defined(ARCH_OS_DARWIN) && defined(ARCH_COMPILER_CLANG))
|
||||
#define ARCH_HAS_THREAD_LOCAL
|
||||
#endif
|
||||
|
||||
// The MAP_POPULATE flag for mmap calls only exists on Linux platforms.
|
||||
#if defined(ARCH_OS_LINUX)
|
||||
#define ARCH_HAS_MMAP_MAP_POPULATE
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_DEFINES_H
|
||||
@@ -0,0 +1,115 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_DEMANGLE_H
|
||||
#define PXR_BASE_ARCH_DEMANGLE_H
|
||||
|
||||
#if !defined(__cplusplus)
|
||||
#error This include file can only be included in C++ programs.
|
||||
#endif
|
||||
|
||||
/// \file arch/demangle.h
|
||||
/// \ingroup group_arch_Strings
|
||||
/// Demangle C++ typenames generated by the \c typeid() facility.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <typeindex>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_Strings
|
||||
///@{
|
||||
|
||||
/// Demangle RTTI-generated type name.
|
||||
///
|
||||
/// Given a variable \c v, the construct \c typeid(v).name() returns
|
||||
/// the type-name of \c v; unfortunately, the returned type-name is
|
||||
/// sometimes extremely encoded (otherwise known as "mangled").
|
||||
/// \c ArchDemangle parses the passed in type-name \c typeName into a
|
||||
/// readable form, and overwrites \c typeName. If \c typeName
|
||||
/// cannot be unmangled, the function returns \c false without altering
|
||||
/// \c typeName. Otherwise \c true is returned.
|
||||
ARCH_API bool ArchDemangle(std::string* typeName);
|
||||
|
||||
/// Return demangled RTTI-generated type name.
|
||||
///
|
||||
/// If \c typeName can be demangled, the function returns the demangled
|
||||
/// string; otherwise, the function returns the empty string.
|
||||
///
|
||||
/// \see ArchDemangle()
|
||||
ARCH_API std::string
|
||||
ArchGetDemangled(const std::string& typeName);
|
||||
|
||||
/// Return demangled RTTI-generated type name.
|
||||
///
|
||||
/// \see ArchDemangle()
|
||||
/// \overload
|
||||
ARCH_API std::string
|
||||
ArchGetDemangled(const char *typeName);
|
||||
|
||||
/// Return demangled RTTI-generated type name.
|
||||
///
|
||||
/// Returns the demangled name associated with typeInfo (i.e. typeInfo.name()).
|
||||
///
|
||||
/// \see ArchDemangle()
|
||||
/// \overload
|
||||
inline std::string
|
||||
ArchGetDemangled(const std::type_info& typeInfo) {
|
||||
return ArchGetDemangled(typeInfo.name());
|
||||
}
|
||||
|
||||
/// Return demangled RTTI-generated type name.
|
||||
///
|
||||
/// Returns the demangled name associated with typeIndex (i.e. typeIndex.name()).
|
||||
///
|
||||
/// \see ArchDemangle()
|
||||
/// \overload
|
||||
inline std::string
|
||||
ArchGetDemangled(const std::type_index& typeIndex) {
|
||||
return ArchGetDemangled(typeIndex.name());
|
||||
}
|
||||
|
||||
/// Return demangled RTTI generated-type name.
|
||||
///
|
||||
/// Returns the demangled name of type T.
|
||||
///
|
||||
/// \see ArchDemangle()
|
||||
/// \overload
|
||||
template <typename T>
|
||||
inline std::string
|
||||
ArchGetDemangled() {
|
||||
return ArchGetDemangled(typeid(T).name());
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/// \private
|
||||
ARCH_API
|
||||
void Arch_DemangleFunctionName(std::string* functionName);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_DEMANGLE_H
|
||||
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_ENV_H
|
||||
#define PXR_BASE_ARCH_ENV_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
///
|
||||
/// Architecture dependent access to environment variables.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
|
||||
///
|
||||
/// Returns \c true if and only if the current environment contains \c name.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
ARCH_API
|
||||
bool
|
||||
ArchHasEnv(const std::string &name);
|
||||
|
||||
///
|
||||
/// Gets a value from the current environment identified by \c name.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
ARCH_API
|
||||
std::string
|
||||
ArchGetEnv(const std::string &name);
|
||||
|
||||
///
|
||||
/// Creates or modifies an environment variable.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
ARCH_API
|
||||
bool
|
||||
ArchSetEnv(const std::string &name, const std::string &value, bool overwrite);
|
||||
|
||||
///
|
||||
/// Removes an environment variable.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
ARCH_API
|
||||
bool
|
||||
ArchRemoveEnv(const std::string &name);
|
||||
|
||||
///
|
||||
/// Expands environment variables in \c str.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
ARCH_API
|
||||
std::string
|
||||
ArchExpandEnvironmentVariables(const std::string& str);
|
||||
|
||||
///
|
||||
/// Return an array of the environment variables.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
ARCH_API
|
||||
char**
|
||||
ArchEnviron();
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_ENV_H
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_ERRNO_H
|
||||
#define PXR_BASE_ARCH_ERRNO_H
|
||||
|
||||
/// \file arch/errno.h
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
/// Functions for dealing with system errors.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_SystemFunctions
|
||||
///@{
|
||||
|
||||
/// Return the error string for the current value of errno.
|
||||
///
|
||||
/// This function provides a thread-safe method of fetching the error string
|
||||
/// from errno. POSIX.1c defines errno as a macro which provides access to a
|
||||
/// thread-local integer. This function is thread-safe.
|
||||
/// \overload
|
||||
ARCH_API std::string ArchStrerror();
|
||||
|
||||
/// Return the error string for the specified value of errno.
|
||||
///
|
||||
/// This function is thread-safe.
|
||||
ARCH_API std::string ArchStrerror(int errorCode);
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
/// Return the error string for the specified error code.
|
||||
///
|
||||
/// This function is thread-safe.
|
||||
ARCH_API std::string ArchStrSysError(unsigned long errorCode);
|
||||
#endif
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_ERRNO_H
|
||||
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_ERROR_H
|
||||
#define PXR_BASE_ARCH_ERROR_H
|
||||
|
||||
/// \file arch/error.h
|
||||
/// \ingroup group_arch_Diagnostics
|
||||
/// Low-level fatal error reporting.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
#include "pxr/base/arch/functionLite.h"
|
||||
#include <stddef.h>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Print message to standard error and abort program.
|
||||
///
|
||||
/// \param msg The reason for the failure.
|
||||
/// \param funcName The name of the function that \c Arch_Error was called from.
|
||||
/// \param lineNo The line number of the file that \c Arch_Error was called from.
|
||||
/// \param fileName The name of the file that \c Arch_Error was called from.
|
||||
///
|
||||
/// \private
|
||||
[[noreturn]]
|
||||
ARCH_API
|
||||
void Arch_Error(const char* msg, const char* funcName,
|
||||
size_t lineNo, const char* fileName);
|
||||
|
||||
/// Print warning message to standard error, but continue execution.
|
||||
///
|
||||
/// \param msg The reason for the warning.
|
||||
/// \param funcName The name of the function that \c Arch_Warning was called from.
|
||||
/// \param lineNo The line number of the file that \c Arch_Warning was called from.
|
||||
/// \param fileName The name of the file that \c Arch_Warning was called from.
|
||||
///
|
||||
/// \private
|
||||
ARCH_API
|
||||
void Arch_Warning(const char* msg, const char* funcName,
|
||||
size_t lineNo, const char* fileName);
|
||||
|
||||
/// \addtogroup group_arch_Diagnostics
|
||||
///@{
|
||||
|
||||
/// Unconditionally aborts the program.
|
||||
///
|
||||
/// \param msg is a literal string, a \c const \c char* (but not
|
||||
/// an \c std::string) that describes why the program is aborting.
|
||||
/// \hideinitializer
|
||||
#define ARCH_ERROR(msg) \
|
||||
Arch_Error(msg, __ARCH_FUNCTION__, __LINE__, __ARCH_FILE__)
|
||||
|
||||
/// Prints a warning message to stderr.
|
||||
///
|
||||
/// \param msg is a literal string, a \c const \c char* (but not
|
||||
/// an \c std::string).
|
||||
/// \hideinitializer
|
||||
#define ARCH_WARNING(msg) \
|
||||
Arch_Warning(msg, __ARCH_FUNCTION__, __LINE__, __ARCH_FILE__)
|
||||
|
||||
/// Aborts the program if \p cond evaluates to false.
|
||||
/// \hideinitializer
|
||||
#define ARCH_AXIOM(cond) \
|
||||
if (!(cond)) ARCH_ERROR("[" #cond "] axiom failed")
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_ERROR_H
|
||||
@@ -0,0 +1,189 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_EXPORT_H
|
||||
#define PXR_BASE_ARCH_EXPORT_H
|
||||
|
||||
/// \file arch/export.h
|
||||
/// \ingroup group_arch_SymbolVisibility
|
||||
///
|
||||
/// Defines symbol visibility macros.
|
||||
///
|
||||
/// Defines \c ARCH_EXPORT to indicate a symbol should be exported from
|
||||
/// a library, \c ARCH_IMPORT to indicate an exported symbol should be
|
||||
/// imported (which only matters on Windows), and \c ARCH_HIDDEN to
|
||||
/// indicate that a symbol which would be exported should not be.
|
||||
///
|
||||
/// The correct way to use these macros is for each library to make a
|
||||
/// header that looks like this (for some hypothetical library named foo):
|
||||
///
|
||||
/// \code
|
||||
/// #ifndef FOO_API_H
|
||||
/// #define FOO_API_H
|
||||
///
|
||||
/// #include "pxr/base/arch/export.h"
|
||||
///
|
||||
/// #if defined(FOO_STATIC)
|
||||
/// # define FOO_API
|
||||
/// # define FOO_API_TEMPLATE_CLASS(...)
|
||||
/// # define FOO_API_TEMPLATE_STRUCT(...)
|
||||
/// # define FOO_LOCAL
|
||||
/// #else
|
||||
/// # if defined(FOO_EXPORTS)
|
||||
/// # define FOO_API ARCH_EXPORT
|
||||
/// # define FOO_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
/// # define FOO_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
/// # else
|
||||
/// # define FOO_API ARCH_IMPORT
|
||||
/// # define FOO_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
/// # define FOO_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
/// # endif
|
||||
/// # define FOO_LOCAL ARCH_HIDDEN
|
||||
/// #endif
|
||||
///
|
||||
/// #endif
|
||||
/// \endcode
|
||||
///
|
||||
/// Note that every library has its own unique _API and _LOCAL macros and
|
||||
/// the expansion of these macros depends on whether the library is being
|
||||
/// built or used (indicated by the externally set macro FOO_EXPORTS).
|
||||
///
|
||||
/// Library headers then include this header and mark classes, methods,
|
||||
/// functions and variables as part of the API or local. Each case is
|
||||
/// described below.
|
||||
///
|
||||
/// A class is added to the API like this:
|
||||
///
|
||||
/// \code
|
||||
/// class FOO_API FooClass ...
|
||||
/// \endcode
|
||||
///
|
||||
/// This will add every member to the API, including implicitly created
|
||||
/// members like a default constructor, default assignment, the vtable
|
||||
/// and the type_info. The type_info is especially important. If you
|
||||
/// will dynamic_cast to the type or catch an exception using the type
|
||||
/// outside of the library, you \b must put its type_info into the API.
|
||||
/// To export the vtable & type_info specifically, use \c ARCH_EXPORT_TYPE.
|
||||
/// However, due to compiler limitations, ARCH_EXPORT_TYPE may put the
|
||||
/// whole class into the API as if ARCH_EXPORT were used.
|
||||
///
|
||||
/// Note that template classes do not get added to the API that way.
|
||||
/// Instead they are added when explicitly instantiated like so:
|
||||
///
|
||||
/// \code
|
||||
/// template class FOO_API FooTemplateClass<FooArgType>;
|
||||
/// \endcode
|
||||
///
|
||||
/// It's also sometimes necessary to indicate that an instantiation exists
|
||||
/// in the API and that a client should not do any of its own instantiation.
|
||||
/// This is necessary, for example, when the template has static data members
|
||||
/// and is done by using extern template in the header file that provides the
|
||||
/// type. Two of the macros above will do that:
|
||||
///
|
||||
/// \code
|
||||
/// FOO_API_TEMPLATE_CLASS(FooTemplateClass<FooArgType>);
|
||||
/// FOO_API_TEMPLATE_STRUCT(FooTemplateClass<FooArgType>);
|
||||
/// \endcode
|
||||
///
|
||||
/// Which you use depends on if FooTemplateClass is a class or struct. The
|
||||
/// macro is used because we don't want to always use extern template, but
|
||||
/// only when we're importing a dynamic library.
|
||||
///
|
||||
/// A template that is completely inlined does not need to be added to the
|
||||
/// API since it's simply instantiated where needed.
|
||||
///
|
||||
/// Functions, methods and variables can also be put into the API:
|
||||
///
|
||||
/// \code
|
||||
/// struct FooLocalClass {
|
||||
/// ~FooLocalClass();
|
||||
/// FOO_API void MoveTheThing();
|
||||
/// };
|
||||
/// FOO_API FooLocalClass* FooNewThing();
|
||||
/// FOO_API extern int doNotUseGlobalVariables;
|
||||
/// \endcode
|
||||
///
|
||||
/// Just because FooLocalClass is not in the API doesn't mean clients can't
|
||||
/// have pointers and references to instances. What they can't do is call
|
||||
/// member functions not in the API (or, as indicated above, use RTTI for
|
||||
/// it). So, for example:
|
||||
///
|
||||
/// \code
|
||||
/// FooLocalClass* thing = FooNewThing();
|
||||
/// thing->MoveTheThing();
|
||||
/// (void)dynamic_cast<FooLocalClass*>(thing); // Link error!
|
||||
/// delete thing; // Link error!
|
||||
/// \endcode
|
||||
///
|
||||
/// Deleting the FooLocalClass instance attempts to use the (non-API) d'tor
|
||||
/// and fails to link. If we had an implicitly defined d'tor then the
|
||||
/// deletion would have worked, presuming FooLocalClass didn't have any
|
||||
/// data members with non-API d'tors.
|
||||
///
|
||||
/// A method of a class added to the API is itself in the API but on some
|
||||
/// platforms (i.e. not Windows) you can remove it from the API using
|
||||
/// FOO_LOCAL:
|
||||
///
|
||||
/// \code
|
||||
/// struct FOO_API FooSemiAPI {
|
||||
/// void DoSomethingPublic();
|
||||
/// FOO_LOCAL void _DoSomethingPrivate();
|
||||
/// };
|
||||
/// \endcode
|
||||
///
|
||||
/// Clients of the library will fail to link if they use _DoSomethingPrivate()
|
||||
/// if FOO_LOCAL is supported. If not then the symbol will remain in the API.
|
||||
|
||||
#include "pxr/base/arch/defines.h"
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# if defined(ARCH_COMPILER_GCC) && ARCH_COMPILER_GCC_MAJOR >= 4 || defined(ARCH_COMPILER_CLANG)
|
||||
# define ARCH_EXPORT __attribute__((dllexport))
|
||||
# define ARCH_IMPORT __attribute__((dllimport))
|
||||
# define ARCH_HIDDEN
|
||||
# define ARCH_EXPORT_TYPE
|
||||
# else
|
||||
# define ARCH_EXPORT __declspec(dllexport)
|
||||
# define ARCH_IMPORT __declspec(dllimport)
|
||||
# define ARCH_HIDDEN
|
||||
# define ARCH_EXPORT_TYPE
|
||||
# endif
|
||||
#elif defined(ARCH_COMPILER_GCC) && ARCH_COMPILER_GCC_MAJOR >= 4 || defined(ARCH_COMPILER_CLANG)
|
||||
# define ARCH_EXPORT __attribute__((visibility("default")))
|
||||
# define ARCH_IMPORT
|
||||
# define ARCH_HIDDEN __attribute__((visibility("hidden")))
|
||||
# if defined(ARCH_COMPILER_CLANG)
|
||||
# define ARCH_EXPORT_TYPE __attribute__((type_visibility("default")))
|
||||
# else
|
||||
# define ARCH_EXPORT_TYPE __attribute__((visibility("default")))
|
||||
# endif
|
||||
#else
|
||||
# define ARCH_EXPORT
|
||||
# define ARCH_IMPORT
|
||||
# define ARCH_HIDDEN
|
||||
# define ARCH_EXPORT_TYPE
|
||||
#endif
|
||||
#define ARCH_EXPORT_TEMPLATE(type, ...)
|
||||
#define ARCH_IMPORT_TEMPLATE(type, ...) extern template type ARCH_IMPORT __VA_ARGS__
|
||||
|
||||
#endif // PXR_BASE_ARCH_EXPORT_H
|
||||
@@ -0,0 +1,428 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_FILE_SYSTEM_H
|
||||
#define PXR_BASE_ARCH_FILE_SYSTEM_H
|
||||
|
||||
/// \file arch/fileSystem.h
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
/// Architecture dependent file system access
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
#include "pxr/base/arch/inttypes.h"
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if defined(ARCH_OS_LINUX)
|
||||
#include <unistd.h>
|
||||
#include <sys/statfs.h>
|
||||
#include <glob.h>
|
||||
#elif defined(ARCH_OS_DARWIN)
|
||||
#include <unistd.h>
|
||||
#include <sys/mount.h>
|
||||
#include <glob.h>
|
||||
#elif defined(ARCH_OS_WINDOWS)
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_SystemFunctions
|
||||
///@{
|
||||
#if !defined(ARCH_OS_WINDOWS)
|
||||
#ifdef _POSIX_VERSION
|
||||
#include <limits.h> /* for PATH_MAX */
|
||||
#else
|
||||
#include <sys/param.h> /* for MAXPATHLEN */
|
||||
#endif
|
||||
#else
|
||||
// XXX -- Should probably have ARCH_ macro for this.
|
||||
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
||||
|
||||
// See https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx
|
||||
// XXX -- Should probably have Arch enum for these.
|
||||
#define F_OK 0 // Test for existence.
|
||||
#define X_OK 1 // Test for execute permission.
|
||||
#define W_OK 2 // Test for write permission.
|
||||
#define R_OK 4 // Test for read permission.
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
#define ARCH_GLOB_NOCHECK 1
|
||||
#define ARCH_GLOB_MARK 2
|
||||
#define ARCH_GLOB_NOSORT 4
|
||||
#else
|
||||
#define ARCH_GLOB_NOCHECK GLOB_NOCHECK
|
||||
#define ARCH_GLOB_MARK GLOB_MARK
|
||||
#define ARCH_GLOB_NOSORT GLOB_NOSORT
|
||||
#endif
|
||||
#define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
|
||||
|
||||
#ifndef ARCH_PATH_MAX
|
||||
#ifdef PATH_MAX
|
||||
#define ARCH_PATH_MAX PATH_MAX
|
||||
#else
|
||||
#ifdef MAXPATHLEN
|
||||
#define ARCH_PATH_MAX MAXPATHLEN
|
||||
#else
|
||||
#ifdef _MAX_PATH
|
||||
#define ARCH_PATH_MAX _MAX_PATH
|
||||
#else
|
||||
#define ARCH_PATH_MAX 1024
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
#define ARCH_PATH_SEP "\\"
|
||||
#define ARCH_PATH_LIST_SEP ";"
|
||||
#define ARCH_REL_PATH_IDENT ".\\"
|
||||
#else
|
||||
#define ARCH_PATH_SEP "/"
|
||||
#define ARCH_PATH_LIST_SEP ":"
|
||||
#define ARCH_REL_PATH_IDENT "./"
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
typedef struct __stat64 ArchStatType;
|
||||
#else
|
||||
typedef struct stat ArchStatType;
|
||||
#endif
|
||||
|
||||
/// \file fileSystem.h
|
||||
/// Architecture dependent file system access
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
|
||||
/// Opens a file.
|
||||
///
|
||||
/// Opens the file that is specified by filename.
|
||||
/// Returning true if the file was opened successfully; false otherwise.
|
||||
///
|
||||
ARCH_API FILE*
|
||||
ArchOpenFile(char const* fileName, char const* mode);
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ArchChmod(path, mode) _chmod(path, mode)
|
||||
#else
|
||||
# define ArchChmod(path, mode) chmod(path, mode)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ArchCloseFile(fd) _close(fd)
|
||||
#else
|
||||
# define ArchCloseFile(fd) close(fd)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ArchUnlinkFile(path) _unlink(path)
|
||||
#else
|
||||
# define ArchUnlinkFile(path) unlink(path)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
ARCH_API int ArchFileAccess(const char* path, int mode);
|
||||
#else
|
||||
# define ArchFileAccess(path, mode) access(path, mode)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ArchFdOpen(fd, mode) _fdopen(fd, mode)
|
||||
#else
|
||||
# define ArchFdOpen(fd, mode) fdopen(fd, mode)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ArchFileNo(stream) _fileno(stream)
|
||||
#else
|
||||
# define ArchFileNo(stream) fileno(stream)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ArchFileIsaTTY(stream) _isatty(stream)
|
||||
#else
|
||||
# define ArchFileIsaTTY(stream) isatty(stream)
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
ARCH_API int ArchRmDir(const char* path);
|
||||
#else
|
||||
# define ArchRmDir(path) rmdir(path)
|
||||
#endif
|
||||
|
||||
/// Return the length of a file in bytes.
|
||||
///
|
||||
/// Returns -1 if the file cannot be opened/read.
|
||||
ARCH_API int64_t ArchGetFileLength(const char* fileName);
|
||||
ARCH_API int64_t ArchGetFileLength(FILE *file);
|
||||
|
||||
/// Return a filename for this file, if one can be obtained.
|
||||
ARCH_API std::string ArchGetFileName(FILE *file);
|
||||
|
||||
/// Returns true if the data in \c stat struct \p st indicates that the target
|
||||
/// file or directory is writable.
|
||||
///
|
||||
/// This returns true if the struct pointer is valid, and the stat indicates
|
||||
/// the target is writable by the effective user, effective group, or all
|
||||
/// users.
|
||||
ARCH_API bool ArchStatIsWritable(const ArchStatType *st);
|
||||
|
||||
/// Returns the modification time (mtime) in seconds for a file.
|
||||
///
|
||||
/// This function stores the modification time with as much precision as is
|
||||
/// available in the stat structure for the current platform in \p time and
|
||||
/// returns \c true on success, otherwise just returns \c false.
|
||||
ARCH_API bool ArchGetModificationTime(const char* pathname, double* time);
|
||||
|
||||
/// Returns the modification time (mtime) in seconds from the stat struct.
|
||||
///
|
||||
/// This function returns the modification time with as much precision as is
|
||||
/// available in the stat structure for the current platform.
|
||||
ARCH_API double ArchGetModificationTime(const ArchStatType& st);
|
||||
|
||||
/// Normalizes the specified path, eliminating double slashes, etc.
|
||||
///
|
||||
/// This canonicalizes paths, removing any double slashes, and eliminiating
|
||||
/// '.', and '..' components of the path. This emulates the behavior of
|
||||
/// os.path.normpath in Python.
|
||||
///
|
||||
/// On Windows, all backslashes are converted to forward slashes and drive
|
||||
/// specifiers (e.g., "C:") are lower-cased. If \p stripDriveSpecifier
|
||||
/// is \c true, these drive specifiers are removed from the path.
|
||||
ARCH_API std::string ArchNormPath(const std::string& path,
|
||||
bool stripDriveSpecifier = false);
|
||||
|
||||
/// Returns the canonical absolute path of the specified filename.
|
||||
///
|
||||
/// This makes the specified path absolute, by prepending the current working
|
||||
/// directory. If the path is already absolute, it is returned unmodified.
|
||||
ARCH_API std::string ArchAbsPath(const std::string& path);
|
||||
|
||||
/// Returns the permissions mode (mode_t) for the given pathname.
|
||||
///
|
||||
/// This function stats the given pathname and returns the permissions flags
|
||||
/// for it and returns true. If the stat fails, returns false.
|
||||
///
|
||||
ARCH_API bool ArchGetStatMode(const char *pathname, int *mode);
|
||||
|
||||
/// Return the path to a temporary directory for this platform.
|
||||
///
|
||||
/// The returned temporary directory will be a location that will normally
|
||||
/// be cleaned out on a reboot. This is /var/tmp on Linux machines (for
|
||||
/// legacy reasons), but /tmp on Darwin machines (/var/tmp on Darwin is
|
||||
/// specified as a location where files are kept between system reboots -
|
||||
/// see "man hier"). The returned string will not have a trailing slash.
|
||||
///
|
||||
/// This routine is threadsafe and will not perform any memory allocations.
|
||||
ARCH_API const char *ArchGetTmpDir();
|
||||
|
||||
/// Make a temporary file name, in a system-determined temporary directory.
|
||||
///
|
||||
/// The result returned has the form TMPDIR/prefix.pid[.n]suffix where TMPDIR
|
||||
/// is a system-determined temporary directory (typically /tmp or /usr/tmp),
|
||||
/// pid is the process id of the process, and the optional .n records the
|
||||
/// number of times this function has been called by a process (and is ommited
|
||||
/// the first time this function is called).
|
||||
///
|
||||
/// The call is threadsafe.
|
||||
///
|
||||
/// \warning This call opens a security hole because of the race between
|
||||
/// choosing the name and opening the file. This call should be avoided in
|
||||
/// favor of \c ArchMakeTmpFile().
|
||||
ARCH_API
|
||||
std::string ArchMakeTmpFileName(const std::string& prefix,
|
||||
const std::string& suffix = std::string());
|
||||
|
||||
/// Create a temporary file, in a system-determined temporary directory.
|
||||
///
|
||||
/// The result returned has the form TMPDIR/prefix.XXXXXX where TMPDIR is a
|
||||
/// system-determined temporary directory (typically /tmp or /usr/tmp) and
|
||||
/// XXXXXX is a unique suffix. Returns the file descriptor of the new file
|
||||
/// and, if pathname isn't NULL, returns the full path to the file in
|
||||
/// pathname. Returns -1 on failure and errno is set.
|
||||
///
|
||||
/// The call is threadsafe.
|
||||
ARCH_API
|
||||
int ArchMakeTmpFile(const std::string& prefix, std::string* pathname = 0);
|
||||
|
||||
/// Create a temporary file, in a given temporary directory.
|
||||
///
|
||||
/// The result returned has the form TMPDIR/prefix.XXXXXX where TMPDIR is the
|
||||
/// given temporary directory and XXXXXX is a unique suffix. Returns the file
|
||||
/// descriptor of the new file and, if pathname isn't NULL, returns the full
|
||||
/// path to the file in pathname. Returns -1 on failure and errno is set.
|
||||
///
|
||||
/// The call is threadsafe.
|
||||
ARCH_API
|
||||
int ArchMakeTmpFile(const std::string& tmpdir,
|
||||
const std::string& prefix, std::string* pathname = 0);
|
||||
|
||||
/// Create a temporary sub-direcrory, in a given temporary directory.
|
||||
///
|
||||
/// The result returned has the form TMPDIR/prefix.XXXXXX/ where TMPDIR is the
|
||||
/// given temporary directory and XXXXXX is a unique suffix. Returns the the
|
||||
/// full path to the subdir in pathname. Returns empty string on failure and
|
||||
/// errno is set.
|
||||
///
|
||||
/// The call is threadsafe.
|
||||
ARCH_API
|
||||
std::string ArchMakeTmpSubdir(const std::string& tmpdir,
|
||||
const std::string& prefix);
|
||||
|
||||
// Helper 'deleter' for use with std::unique_ptr for file mappings.
|
||||
struct Arch_Unmapper {
|
||||
Arch_Unmapper() : _length(~0) {}
|
||||
explicit Arch_Unmapper(size_t length) : _length(length) {}
|
||||
ARCH_API void operator()(char *mapStart) const;
|
||||
ARCH_API void operator()(char const *mapStart) const;
|
||||
size_t GetLength() const { return _length; }
|
||||
private:
|
||||
size_t _length;
|
||||
};
|
||||
|
||||
/// ArchConstFileMapping and ArchMutableFileMapping are std::unique_ptr<char
|
||||
/// const *, ...> and std::unique_ptr<char *, ...> respectively. The functions
|
||||
/// ArchMapFileReadOnly() and ArchMapFileReadWrite() return them and provide
|
||||
/// access to memory-mapped file contents.
|
||||
using ArchConstFileMapping = std::unique_ptr<char const, Arch_Unmapper>;
|
||||
using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
|
||||
|
||||
/// Return the length of an ArchConstFileMapping.
|
||||
inline size_t
|
||||
ArchGetFileMappingLength(ArchConstFileMapping const &m) {
|
||||
return m.get_deleter().GetLength();
|
||||
}
|
||||
|
||||
/// Return the length of an ArchMutableFileMapping.
|
||||
inline size_t
|
||||
ArchGetFileMappingLength(ArchMutableFileMapping const &m) {
|
||||
return m.get_deleter().GetLength();
|
||||
}
|
||||
|
||||
/// Privately map the passed \p file into memory and return a unique_ptr to the
|
||||
/// read-only mapped contents. The contents may not be modified. If mapping
|
||||
/// fails, return a null unique_ptr and if errMsg is not null fill it with
|
||||
/// information about the failure.
|
||||
ARCH_API
|
||||
ArchConstFileMapping
|
||||
ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr);
|
||||
|
||||
/// \overload
|
||||
ARCH_API
|
||||
ArchConstFileMapping
|
||||
ArchMapFileReadOnly(std::string const& path, std::string *errMsg=nullptr);
|
||||
|
||||
/// Privately map the passed \p file into memory and return a unique_ptr to the
|
||||
/// copy-on-write mapped contents. If modified, the affected pages are
|
||||
/// dissociated from the underlying file and become backed by the system's swap
|
||||
/// or page-file storage. Edits are not carried through to the underlying file.
|
||||
/// If mapping fails, return a null unique_ptr and if errMsg is not null fill it
|
||||
/// with information about the failure.
|
||||
ARCH_API
|
||||
ArchMutableFileMapping
|
||||
ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr);
|
||||
|
||||
/// \overload
|
||||
ARCH_API
|
||||
ArchMutableFileMapping
|
||||
ArchMapFileReadWrite(std::string const& path, std::string *errMsg=nullptr);
|
||||
|
||||
enum ArchMemAdvice {
|
||||
ArchMemAdviceNormal, // Treat range with default behavior.
|
||||
ArchMemAdviceWillNeed, // OS may prefetch this range.
|
||||
ArchMemAdviceDontNeed, // OS may free resources related to this range.
|
||||
ArchMemAdviceRandomAccess, // Prefetching may not be beneficial.
|
||||
};
|
||||
|
||||
/// Advise the OS regarding how the application intends to access a range of
|
||||
/// memory. See ArchMemAdvice. This is primarily useful for mapped file
|
||||
/// regions. This call does not change program semantics. It is only an
|
||||
/// optimization hint to the OS, and may be a no-op on some systems.
|
||||
ARCH_API
|
||||
void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv);
|
||||
|
||||
/// Report whether or not the mapped virtual memory pages starting at \p addr
|
||||
/// for \p len bytes are resident in RAM. Pages that are resident will not,
|
||||
/// when accessed, cause a page fault while those that are not will. Return
|
||||
/// true on success and false in case of an error. The \p addr argument must be
|
||||
/// a multiple of ArchGetPageSize(). The \p len argument need not be a multiple
|
||||
/// of the page size; it will be rounded up to the next page boundary. Fill
|
||||
/// \p pageMap with 0s for pages not resident in memory and 1s for pages that
|
||||
/// are. The \p pageMap argument must therefore point to at least (\p len +
|
||||
/// ArchGetPageSize()-1)/ArchGetPageSize() bytes.
|
||||
///
|
||||
/// Note that currently this function is only implemented on Linux and Darwin.
|
||||
/// On Windows it currently always returns false.
|
||||
ARCH_API
|
||||
bool
|
||||
ArchQueryMappedMemoryResidency(
|
||||
void const *addr, size_t len, unsigned char *pageMap);
|
||||
|
||||
/// Read up to \p count bytes from \p offset in \p file into \p buffer. The
|
||||
/// file position indicator for \p file is not changed. Return the number of
|
||||
/// bytes read, or zero if at end of file. Return -1 in case of an error, with
|
||||
/// errno set appropriately.
|
||||
ARCH_API
|
||||
int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset);
|
||||
|
||||
/// Write up to \p count bytes from \p buffer to \p file at \p offset. The file
|
||||
/// position indicator for \p file is not changed. Return the number of bytes
|
||||
/// written, possibly zero if none written. Return -1 in case of an error, with
|
||||
/// errno set appropriately.
|
||||
ARCH_API
|
||||
int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset);
|
||||
|
||||
/// Returns the value of the symbolic link at \p path. Returns the empty
|
||||
/// string on error or if \p path does not refer to a symbolic link.
|
||||
ARCH_API
|
||||
std::string ArchReadLink(const char* path);
|
||||
|
||||
enum ArchFileAdvice {
|
||||
ArchFileAdviceNormal, // Treat range with default behavior.
|
||||
ArchFileAdviceWillNeed, // OS may prefetch this range.
|
||||
ArchFileAdviceDontNeed, // OS may free resources related to this range.
|
||||
ArchFileAdviceRandomAccess, // Prefetching may not be beneficial.
|
||||
};
|
||||
|
||||
/// Advise the OS regarding how the application intends to access a range of
|
||||
/// bytes in a file. See ArchFileAdvice. This call does not change program
|
||||
/// semantics. It is only an optimization hint to the OS, and may be a no-op on
|
||||
/// some systems.
|
||||
ARCH_API
|
||||
void ArchFileAdvise(FILE *file, int64_t offset, size_t count,
|
||||
ArchFileAdvice adv);
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_FILE_SYSTEM_H
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_FUNCTION_H
|
||||
#define PXR_BASE_ARCH_FUNCTION_H
|
||||
|
||||
/// \file arch/function.h
|
||||
/// Define preprocessor function name macros.
|
||||
///
|
||||
/// This file extents the functionality of pxr/base/arch/functionLite.h.
|
||||
/// This file needs to be public but shouldn't be included directly by
|
||||
/// anything outside of \c lib/tf.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/functionLite.h"
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Return well formatted function name.
|
||||
///
|
||||
/// This function assumes \c function is __ARCH_FUNCTION__ and
|
||||
/// \c prettyFunction is __ARCH_PRETTY_FUNCTION__, and attempts to
|
||||
/// reconstruct a well formatted function name.
|
||||
///
|
||||
/// \ingroup group_arch_Diagnostic
|
||||
ARCH_API
|
||||
std::string ArchGetPrettierFunctionName(const std::string &function,
|
||||
const std::string &prettyFunction);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_FUNCTION_H
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_FUNCTION_LITE_H
|
||||
#define PXR_BASE_ARCH_FUNCTION_LITE_H
|
||||
|
||||
/// \file arch/functionLite.h
|
||||
/// Define preprocessor function name macros.
|
||||
///
|
||||
/// This file defines preprocessor macros for getting the current function
|
||||
/// name and related information so they can be used in a architecture
|
||||
/// independent manner. This file needs to be public but shouldn't be
|
||||
/// included directly by anything outside of \c pxr/base/tf.
|
||||
|
||||
#include "pxr/base/arch/defines.h"
|
||||
// Note: this file specifically does not include <string>.
|
||||
|
||||
#define __ARCH_FUNCTION__ __func__
|
||||
|
||||
#if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_ICC) || \
|
||||
defined(ARCH_COMPILER_CLANG)
|
||||
# define __ARCH_PRETTY_FUNCTION__ __PRETTY_FUNCTION__
|
||||
#elif defined(ARCH_COMPILER_MSVC)
|
||||
# define __ARCH_PRETTY_FUNCTION__ __FUNCSIG__
|
||||
#else
|
||||
# define __ARCH_PRETTY_FUNCTION__ __ARCH_FUNCTION__
|
||||
#endif /* defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_ICC) ||
|
||||
defined(ARCH_COMPILER_CLANG)*/
|
||||
|
||||
#if defined(BUILD_COMPONENT_SRC_PREFIX)
|
||||
# define __ARCH_FILE__ BUILD_COMPONENT_SRC_PREFIX __FILE__
|
||||
#else
|
||||
# define __ARCH_FILE__ __FILE__
|
||||
#endif /* defined(BUILD_COMPONENT_SRC_PREFIX) */
|
||||
|
||||
#endif // PXR_BASE_ARCH_FUNCTION_LITE_H
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_HASH_H
|
||||
#define PXR_BASE_ARCH_HASH_H
|
||||
|
||||
/// \file arch/hash.h
|
||||
/// Hash functions.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/inttypes.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Hash \a len bytes of \a data.
|
||||
///
|
||||
/// To compute a hash value for data that is not contiguous in memory, iterate
|
||||
/// over all the contiguous blocks of memory and accumulate the hash value by
|
||||
/// passing it on as \p seed. Note that this is *not* equivalent to hashing the
|
||||
/// contiguous pieces as a whole. Support for that may be added in future.
|
||||
///
|
||||
ARCH_API uint32_t ArchHash(const char *data, size_t len);
|
||||
/// \overload
|
||||
ARCH_API uint32_t ArchHash(const char *data, size_t len, uint32_t seed);
|
||||
|
||||
/// Hash \a len bytes of \a data.
|
||||
///
|
||||
/// To compute a hash value for data that is not contiguous in memory, iterate
|
||||
/// over all the contiguous blocks of memory and accumulate the hash value by
|
||||
/// passing it on as \p seed. Note that this is *not* equivalent to hashing the
|
||||
/// contiguous pieces as a whole. Support for that may be added in future.
|
||||
///
|
||||
ARCH_API uint64_t ArchHash64(const char *data, size_t len);
|
||||
/// \overload
|
||||
ARCH_API uint64_t ArchHash64(const char *data, size_t len, uint64_t seed);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_HASH_H
|
||||
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_HINTS_H
|
||||
#define PXR_BASE_ARCH_HINTS_H
|
||||
|
||||
#include "pxr/base/arch/defines.h"
|
||||
|
||||
/// \file arch/hints.h
|
||||
/// Compiler hints.
|
||||
///
|
||||
/// \c ARCH_LIKELY(bool-expr) and \c ARCH_UNLIKELY(bool-expr) will evaluate to
|
||||
/// the value of bool-expr but will also emit compiler intrinsics providing
|
||||
/// hints for branch prediction if the compiler has such intrinsics. It is
|
||||
/// advised that you only use these in cases where you empirically know the
|
||||
/// outcome of bool-expr to a very high degree of certainty. For example,
|
||||
/// fatal-error cases, invariants, first-time initializations, etc.
|
||||
|
||||
#if defined(ARCH_COMPILER_GCC) || defined(ARCH_COMPILER_CLANG)
|
||||
|
||||
#define ARCH_LIKELY(x) (__builtin_expect((bool)(x), true))
|
||||
#define ARCH_UNLIKELY(x) (__builtin_expect((bool)(x), false))
|
||||
|
||||
#else
|
||||
|
||||
#define ARCH_LIKELY(x) (x)
|
||||
#define ARCH_UNLIKELY(x) (x)
|
||||
|
||||
#endif
|
||||
|
||||
#endif // PXR_BASE_ARCH_HINTS_H
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_INTTYPES_H
|
||||
#define PXR_BASE_ARCH_INTTYPES_H
|
||||
|
||||
/// \file arch/inttypes.h
|
||||
/// \ingroup group_arch_Bits
|
||||
/// Define integral types.
|
||||
///
|
||||
/// By including this file, the "standard" integer types \c int16_t,
|
||||
/// \c int32_t, and \c int64_t are all defined, as are their unsigned
|
||||
/// counterparts \c uint16_t, \c uint32_t, and \c uint64_t. This also
|
||||
/// includes the macros for limits, constants, and printf format specifiers.
|
||||
|
||||
// These defines should in theory not be needed to get the related sized-int
|
||||
// macros, as this was not adopted by the C++ committee and was dropped by the C
|
||||
// committee, but glibc erroneously "respects" them so we need to have them.
|
||||
#ifndef __STDC_LIMIT_MACROS
|
||||
#define __STDC_LIMIT_MACROS
|
||||
#endif
|
||||
#ifndef __STDC_CONSTANT_MACROS
|
||||
#define __STDC_CONSTANT_MACROS
|
||||
#endif
|
||||
#ifndef __STDC_FORMAT_MACROS
|
||||
#define __STDC_FORMAT_MACROS
|
||||
#endif
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstdint>
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
typedef unsigned char uchar;
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_INTTYPES_H
|
||||
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_LIBRARY_H
|
||||
#define PXR_BASE_ARCH_LIBRARY_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#if defined(ARCH_OS_WINDOWS)
|
||||
# define ARCH_LIBRARY_LAZY 0
|
||||
# define ARCH_LIBRARY_NOW 0
|
||||
# define ARCH_LIBRARY_LOCAL 0
|
||||
# define ARCH_LIBRARY_GLOBAL 0
|
||||
# define ARCH_LIBRARY_SUFFIX ".dll"
|
||||
# define ARCH_STATIC_LIBRARY_SUFFIX ".lib"
|
||||
#else
|
||||
# include <dlfcn.h>
|
||||
# define ARCH_LIBRARY_LAZY RTLD_LAZY
|
||||
# define ARCH_LIBRARY_NOW RTLD_NOW
|
||||
# define ARCH_LIBRARY_LOCAL RTLD_LOCAL
|
||||
# define ARCH_LIBRARY_GLOBAL RTLD_GLOBAL
|
||||
# if defined(ARCH_OS_DARWIN)
|
||||
# define ARCH_LIBRARY_SUFFIX ".dylib"
|
||||
# else
|
||||
# define ARCH_LIBRARY_SUFFIX ".so"
|
||||
# endif
|
||||
# define ARCH_STATIC_LIBRARY_SUFFIX ".a"
|
||||
#endif
|
||||
|
||||
// On MacOS shared libraries and loadable modules (aka loadable bundles aka
|
||||
// plugins) are different entities. Most cross-platform software packages
|
||||
// that create loadable modules use .so as the extension on MacOS for
|
||||
// compatibility, so we use that here.
|
||||
#if defined(ARCH_OS_DARWIN)
|
||||
# define ARCH_PLUGIN_SUFFIX ".so"
|
||||
#else
|
||||
# define ARCH_PLUGIN_SUFFIX ARCH_LIBRARY_SUFFIX
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// library.h
|
||||
/// Architecture dependent loading and unloading of dynamic libraries.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
|
||||
/// Load an executable object file.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
/// Opens the dynamic library that is specified by filename.
|
||||
/// Returning the handle to the module if successful; false otherwise.
|
||||
ARCH_API
|
||||
void* ArchLibraryOpen(const std::string &filename, int flag);
|
||||
|
||||
/// Obtain a description of the most recent error that occurred from
|
||||
/// \c ArchLibraryOpen.
|
||||
///\ingroup group_arch_SystemFunctions
|
||||
ARCH_API
|
||||
std::string ArchLibraryError();
|
||||
|
||||
/// Closes an object opened with \c ArchLibraryOpen.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
ARCH_API
|
||||
int ArchLibraryClose(void* handle);
|
||||
|
||||
/// Obtain the address of a symbol defined within an object opened with
|
||||
/// \c ArchLibraryOpen.
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
///
|
||||
/// Obtain the address of a symbol that is specified by name.
|
||||
/// Returning the address of the symbol if successful; nullptr otherwise.
|
||||
ARCH_API
|
||||
void* ArchLibraryGetSymbolAddress(void* handle, const char* name);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_LIBRARY_H
|
||||
@@ -0,0 +1,160 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_MALLOC_HOOK_H
|
||||
#define PXR_BASE_ARCH_MALLOC_HOOK_H
|
||||
|
||||
/// \file arch/mallocHook.h
|
||||
/// \ingroup group_arch_Memory
|
||||
/// Routines for controlling malloc behavior.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Return true if ptmalloc is being used as the memory allocator
|
||||
///
|
||||
/// ptmalloc3 is an external shared library providing implementations of the
|
||||
/// standard memory allocation functions (e.g. malloc, free). Consumers with
|
||||
/// special behavior that depends on this library may use this function to
|
||||
/// determine if it is the active allocator.
|
||||
///
|
||||
/// \ingroup group_arch_Memory
|
||||
ARCH_API bool ArchIsPtmallocActive();
|
||||
|
||||
/// Return true if the C++ STL allocator was requested to be turned off.
|
||||
///
|
||||
/// Under gcc, this is done by setting the environment variable
|
||||
/// GLIBCXX_FORCE_NEW, but it might differ (or not even be possible) for other
|
||||
/// platforms.
|
||||
///
|
||||
/// \ingroup group_arch_Memory
|
||||
ARCH_API bool ArchIsStlAllocatorOff();
|
||||
|
||||
/// \class ArchMallocHook
|
||||
/// \ingroup group_arch_Memory
|
||||
///
|
||||
/// Override default malloc() functionality.
|
||||
///
|
||||
/// The \c ArchMallocHook class is used on supported systems to install a
|
||||
/// call-back function in place of the standard malloc/realloc/free/memalign
|
||||
/// function calls. Supported systems are currently restricted to 64-bit linux
|
||||
/// systems.
|
||||
///
|
||||
/// The call-back function can access the original allocation function by
|
||||
/// calling, for example, \c ArchMallocHook::Malloc, or it is free to perform
|
||||
/// its own allocation.
|
||||
///
|
||||
/// The \c ArchMallocHook is a POD (plain old datastructure) which means that
|
||||
/// to use it properly, it should be declared at global scope, ensuring
|
||||
/// zero-initialization.
|
||||
class ArchMallocHook {
|
||||
public:
|
||||
/// Initialize hooks.
|
||||
///
|
||||
/// Calling \c Initialize() installs the supplied functions as call-back
|
||||
/// in place of the standard system memory allocation routines. Note
|
||||
/// that the callbacks take an extra \c const \c void* parameter; on
|
||||
/// supported systems, the called function should simply ignore the extra
|
||||
/// parameter.
|
||||
///
|
||||
/// If initialization fails, \c false is returned and \p *errMsg is set
|
||||
/// accordingly. If \c *this has already been initialized, calling \c
|
||||
/// Initialize() a second time will fail.
|
||||
ARCH_API
|
||||
bool Initialize(void* (*mallocWrapper)(size_t, const void*),
|
||||
void* (*reallocWrapper)(void*, size_t, const void*),
|
||||
void* (*memalignWrapper)(size_t, size_t, const void*),
|
||||
void (*freeWrapper)(void*, const void*),
|
||||
std::string* errMsg);
|
||||
|
||||
/// Return true if \c *this has been (successfully) initialized.
|
||||
///
|
||||
/// In order for this function to work properly, \c this cannot be a local
|
||||
/// or dynamically initialized variable; rather, \c this must be a global
|
||||
/// variable, to ensure zero-initialization.
|
||||
ARCH_API
|
||||
bool IsInitialized();
|
||||
|
||||
/// Call the original system \c malloc() function.
|
||||
///
|
||||
/// This function allows user-supplied callbacks to access the original
|
||||
/// system-supplied malloc() call. For speed reasons, no safety checks
|
||||
/// are performed; in particular, calling this function without having
|
||||
/// successfully initialized \p *this will likely crash your program.
|
||||
ARCH_API
|
||||
void* Malloc(size_t nBytes) {
|
||||
return (*_underlyingMallocFunc)(nBytes);
|
||||
}
|
||||
|
||||
/// Call the original system \c realloc() function.
|
||||
///
|
||||
/// This function allows user-supplied callbacks to access the original
|
||||
/// system-supplied \c realloc() call. For speed reasons, no safety checks
|
||||
/// are performed; in particular, calling this function without having
|
||||
/// successfully initialized \p *this will likely crash your program.
|
||||
ARCH_API
|
||||
void* Realloc(void* ptr, size_t nBytes) {
|
||||
return (*_underlyingReallocFunc)(ptr, nBytes);
|
||||
}
|
||||
|
||||
/// Call the original system \c memalign() function.
|
||||
///
|
||||
/// This function allows user-supplied callbacks to access the original
|
||||
/// system-supplied \c memalign() call. For speed reasons, no safety checks
|
||||
/// are performed; in particular, calling this function without having
|
||||
/// successfully initialized \p *this will likely crash your program.
|
||||
ARCH_API
|
||||
void* Memalign(size_t alignment, size_t nBytes) {
|
||||
return (*_underlyingMemalignFunc)(alignment, nBytes);
|
||||
}
|
||||
|
||||
/// Call the original system \c free() function.
|
||||
///
|
||||
/// This function allows user-supplied callbacks to access the original
|
||||
/// system-supplied \c free() call. For speed reasons, no safety checks
|
||||
/// are performed; in particular, calling this function without having
|
||||
/// successfully initialized \p *this will likely crash your program.
|
||||
ARCH_API
|
||||
void Free(void* ptr) {
|
||||
(*_underlyingFreeFunc)(ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
// Note: this is a POD (plain 'ol data structure) so we depend on zero
|
||||
// initialization here to null these out. Do not add a constructor or
|
||||
// destructor to this class.
|
||||
|
||||
void* (*_underlyingMallocFunc)(size_t);
|
||||
void* (*_underlyingReallocFunc)(void*, size_t);
|
||||
void* (*_underlyingMemalignFunc)(size_t, size_t);
|
||||
void (*_underlyingFreeFunc)(void*);
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_MALLOC_HOOK_H
|
||||
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_MATH_H
|
||||
#define PXR_BASE_ARCH_MATH_H
|
||||
|
||||
/// \file arch/math.h
|
||||
/// \ingroup group_arch_Math
|
||||
/// Architecture-specific math function calls.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
#include "pxr/base/arch/inttypes.h"
|
||||
|
||||
#include <cmath>
|
||||
#if !defined(M_PI)
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_Math
|
||||
///@{
|
||||
|
||||
#if defined (ARCH_CPU_INTEL) || defined (ARCH_CPU_ARM) || defined (doxygen)
|
||||
|
||||
/// This is the smallest value e such that 1+e^2 == 1, using floats.
|
||||
/// True for all IEEE754 chipsets.
|
||||
#define ARCH_MIN_FLOAT_EPS_SQR 0.000244141F
|
||||
|
||||
/// Three-valued sign. Return 1 if val > 0, 0 if val == 0, or -1 if val < 0.
|
||||
inline long ArchSign(long val) {
|
||||
return (val > 0) - (val < 0);
|
||||
}
|
||||
|
||||
/// Returns The IEEE-754 bit pattern of the specified single precision value
|
||||
/// as a 32-bit unsigned integer.
|
||||
inline uint32_t ArchFloatToBitPattern(float v) {
|
||||
union {
|
||||
float _float;
|
||||
uint32_t _uint;
|
||||
} value;
|
||||
value._float = v;
|
||||
return value._uint;
|
||||
}
|
||||
|
||||
/// Returns The single precision floating point value corresponding to the
|
||||
/// given IEEE-754 bit pattern.
|
||||
inline float ArchBitPatternToFloat(uint32_t v) {
|
||||
union {
|
||||
uint32_t _uint;
|
||||
float _float;
|
||||
} value;
|
||||
value._uint = v;
|
||||
return value._float;
|
||||
}
|
||||
|
||||
/// Returns The IEEE-754 bit pattern of the specified double precision value
|
||||
/// as a 64-bit unsigned integer.
|
||||
inline uint64_t ArchDoubleToBitPattern(double v) {
|
||||
union {
|
||||
double _double;
|
||||
uint64_t _uint;
|
||||
} value;
|
||||
value._double = v;
|
||||
return value._uint;
|
||||
}
|
||||
|
||||
/// Returns The double precision floating point value corresponding to the
|
||||
/// given IEEE-754 bit pattern.
|
||||
inline double ArchBitPatternToDouble(uint64_t v) {
|
||||
union {
|
||||
uint64_t _uint;
|
||||
double _double;
|
||||
} value;
|
||||
value._uint = v;
|
||||
return value._double;
|
||||
}
|
||||
|
||||
#else
|
||||
#error Unknown system architecture.
|
||||
#endif
|
||||
|
||||
#if defined(ARCH_OS_LINUX) || defined(doxygen)
|
||||
|
||||
/// Computes the sine and cosine of the specified value as a float.
|
||||
inline void ArchSinCosf(float v, float *s, float *c) { sincosf(v, s, c); }
|
||||
|
||||
/// Computes the sine and cosine of the specified value as a double.
|
||||
inline void ArchSinCos(double v, double *s, double *c) { sincos(v, s, c); }
|
||||
|
||||
#elif defined(ARCH_OS_DARWIN) || defined(ARCH_OS_WINDOWS)
|
||||
|
||||
inline void ArchSinCosf(float v, float *s, float *c) {
|
||||
*s = std::sin(v);
|
||||
*c = std::cos(v);
|
||||
}
|
||||
inline void ArchSinCos(double v, double *s, double *c) {
|
||||
*s = std::sin(v);
|
||||
*c = std::cos(v);
|
||||
}
|
||||
|
||||
#else
|
||||
#error Unknown architecture.
|
||||
#endif
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_MATH_H
|
||||
@@ -0,0 +1,209 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_PRAGMAS_H
|
||||
#define PXR_BASE_ARCH_PRAGMAS_H
|
||||
|
||||
/// \file arch/pragmas.h
|
||||
/// Pragmas for controlling compiler-specific behaviors.
|
||||
///
|
||||
/// This header contains pragmas used to control compiler-specific behaviors.
|
||||
/// Behaviors that are not supported or required by a certain compiler should
|
||||
/// be implemented as a no-op.
|
||||
|
||||
#include "pxr/base/arch/defines.h"
|
||||
|
||||
#if defined(ARCH_COMPILER_GCC)
|
||||
|
||||
#define ARCH_PRAGMA_PUSH \
|
||||
_Pragma("GCC diagnostic push")
|
||||
|
||||
#define ARCH_PRAGMA_POP \
|
||||
_Pragma("GCC diagnostic pop")
|
||||
|
||||
// Convert errors about variables that may be used before initialization
|
||||
// into warnings.
|
||||
//
|
||||
// This works around GCC bug 47679.
|
||||
#define ARCH_PRAGMA_MAYBE_UNINITIALIZED \
|
||||
_Pragma("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
|
||||
|
||||
#define ARCH_PRAGMA_MACRO_REDEFINITION \
|
||||
_Pragma("GCC diagnostic ignored \"-Wbuiltin-macro-redefined\"")
|
||||
|
||||
#define ARCH_PRAGMA_WRITE_STRINGS \
|
||||
_Pragma("GCC diagnostic ignored \"-Wwrite-strings\"")
|
||||
|
||||
#elif defined(ARCH_COMPILER_CLANG)
|
||||
|
||||
#define ARCH_PRAGMA_PUSH \
|
||||
_Pragma("clang diagnostic push")
|
||||
|
||||
#define ARCH_PRAGMA_POP \
|
||||
_Pragma("clang diagnostic pop")
|
||||
|
||||
#define ARCH_PRAGMA_MACRO_REDEFINITION \
|
||||
_Pragma("clang diagnostic ignored \"-Wbuiltin-macro-redefined\"")
|
||||
|
||||
#define ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE \
|
||||
_Pragma("clang diagnostic ignored \"-Wundefined-var-template\"")
|
||||
|
||||
#define ARCH_PRAGMA_WRITE_STRINGS \
|
||||
_Pragma("clang diagnostic ignored \"-Wwrite-strings\"")
|
||||
|
||||
#define ARCH_PRAGMA_INSTANTIATION_AFTER_SPECIALIZATION \
|
||||
_Pragma("clang diagnostic ignored \"-Winstantiation-after-specialization\"")
|
||||
|
||||
#define ARCH_PRAGMA_INSTANCE_METHOD_NOT_FOUND \
|
||||
_Pragma("clang diagnostic ignored \"-Wobjc-method-access\"")
|
||||
|
||||
#elif defined(ARCH_COMPILER_MSVC)
|
||||
|
||||
#define ARCH_PRAGMA_PUSH \
|
||||
__pragma(warning(push))
|
||||
|
||||
#define ARCH_PRAGMA_POP \
|
||||
__pragma(warning(pop))
|
||||
|
||||
#define ARCH_PRAGMA_MACRO_TOO_FEW_ARGUMENTS \
|
||||
__pragma(warning(disable:4003))
|
||||
|
||||
#define ARCH_PRAGMA_MACRO_REDEFINITION \
|
||||
__pragma(warning(disable:4005))
|
||||
|
||||
#define ARCH_PRAGMA_QUALIFIER_HAS_NO_MEANING \
|
||||
__pragma(warning(disable:4180))
|
||||
|
||||
#define ARCH_PRAGMA_ZERO_SIZED_STRUCT \
|
||||
__pragma(warning(disable:4200))
|
||||
|
||||
#define ARCH_PRAGMA_NEEDS_EXPORT_INTERFACE \
|
||||
__pragma(warning(disable:4251))
|
||||
|
||||
#define ARCH_PRAGMA_CONVERSION_FROM_SIZET \
|
||||
__pragma(warning(disable:4267))
|
||||
|
||||
#define ARCH_PRAGMA_MAY_NOT_BE_ALIGNED \
|
||||
__pragma(warning(disable:4316))
|
||||
|
||||
#define ARCH_PRAGMA_SHIFT_TO_64_BITS \
|
||||
__pragma(warning(disable:4334))
|
||||
|
||||
#define ARCH_PRAGMA_DESTRUCTOR_IMPLICIT_DEFINE \
|
||||
__pragma(warning(disable:4624))
|
||||
|
||||
#define ARCH_PRAGMA_DEPRECATED_POSIX_NAME \
|
||||
__pragma(warning(disable:4996))
|
||||
|
||||
#define ARCH_PRAGMA_FORCING_TO_BOOL \
|
||||
__pragma(warning(disable:4800))
|
||||
|
||||
#define ARCH_PRAGMA_UNSAFE_USE_OF_BOOL \
|
||||
__pragma(warning(disable:4804))
|
||||
|
||||
#define ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED \
|
||||
__pragma(warning(disable:4146))
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_PUSH
|
||||
#define ARCH_PRAGMA_PUSH
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_POP
|
||||
#define ARCH_PRAGMA_POP
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_MAYBE_UNINITIALIZED
|
||||
#define ARCH_PRAGMA_MAYBE_UNINITIALIZED
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_MACRO_REDEFINITION
|
||||
#define ARCH_PRAGMA_MACRO_REDEFINITION
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_WRITE_STRINGS
|
||||
#define ARCH_PRAGMA_WRITE_STRINGS
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_MACRO_TOO_FEW_ARGUMENTS
|
||||
#define ARCH_PRAGMA_MACRO_TOO_FEW_ARGUMENTS
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_QUALIFIER_HAS_NO_MEANING
|
||||
#define ARCH_PRAGMA_QUALIFIER_HAS_NO_MEANING
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_ZERO_SIZED_STRUCT
|
||||
#define ARCH_PRAGMA_ZERO_SIZED_STRUCT
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_NEEDS_EXPORT_INTERFACE
|
||||
#define ARCH_PRAGMA_NEEDS_EXPORT_INTERFACE
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_CONVERSION_FROM_SIZET
|
||||
#define ARCH_PRAGMA_CONVERSION_FROM_SIZET
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_MAY_NOT_BE_ALIGNED
|
||||
#define ARCH_PRAGMA_MAY_NOT_BE_ALIGNED
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_SHIFT_TO_64_BITS
|
||||
#define ARCH_PRAGMA_SHIFT_TO_64_BITS
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_DESTRUCTOR_IMPLICIT_DEFINE
|
||||
#define ARCH_PRAGMA_DESTRUCTOR_IMPLICIT_DEFINE
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_DEPRECATED_POSIX_NAME
|
||||
#define ARCH_PRAGMA_DEPRECATED_POSIX_NAME
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_FORCING_TO_BOOL
|
||||
#define ARCH_PRAGMA_FORCING_TO_BOOL
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
|
||||
#define ARCH_PRAGMA_UNSAFE_USE_OF_BOOL
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
|
||||
#define ARCH_PRAGMA_UNARY_MINUS_ON_UNSIGNED
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_INSTANTIATION_AFTER_SPECIALIZATION
|
||||
#define ARCH_PRAGMA_INSTANTIATION_AFTER_SPECIALIZATION
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
|
||||
#define ARCH_PRAGMA_UNDEFINED_VAR_TEMPLATE
|
||||
#endif
|
||||
|
||||
#if !defined ARCH_PRAGMA_INSTANCE_METHOD_NOT_FOUND
|
||||
#define ARCH_PRAGMA_INSTANCE_METHOD_NOT_FOUND
|
||||
#endif
|
||||
|
||||
#endif // PXR_BASE_ARCH_PRAGMAS_H
|
||||
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// Copyright 2017 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_REGEX_H
|
||||
#define PXR_BASE_ARCH_REGEX_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class ArchRegex {
|
||||
public:
|
||||
static constexpr unsigned int CASE_INSENSITIVE = 1u;
|
||||
static constexpr unsigned int GLOB = 2u;
|
||||
|
||||
/// Create an empty regex.
|
||||
ARCH_API ArchRegex();
|
||||
ARCH_API ArchRegex(const std::string& pattern, unsigned int flags = 0);
|
||||
ArchRegex(const ArchRegex&) = delete;
|
||||
ARCH_API ArchRegex(ArchRegex&&) noexcept;
|
||||
ArchRegex& operator=(const ArchRegex&) = delete;
|
||||
ARCH_API ArchRegex& operator=(ArchRegex&&) noexcept;
|
||||
ARCH_API ~ArchRegex();
|
||||
|
||||
/// Returns \c true if the regex is valid.
|
||||
ARCH_API explicit operator bool() const;
|
||||
|
||||
/// Returns the reason the regex is invalid or the empty string if
|
||||
/// it's valid.
|
||||
ARCH_API std::string GetError() const;
|
||||
|
||||
/// Returns the flags used to construct the regex.
|
||||
ARCH_API unsigned int GetFlags() const;
|
||||
|
||||
/// Returns \c true if the regex matches \p query anywhere, otherwise
|
||||
/// returns \c false.
|
||||
ARCH_API bool Match(const std::string& query) const;
|
||||
|
||||
private:
|
||||
class _Impl;
|
||||
unsigned int _flags = 0;
|
||||
std::string _error;
|
||||
std::unique_ptr<_Impl> _impl;
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_REGEX_H
|
||||
@@ -0,0 +1,329 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_STACK_TRACE_H
|
||||
#define PXR_BASE_ARCH_STACK_TRACE_H
|
||||
|
||||
/// \file arch/stackTrace.h
|
||||
/// \ingroup group_arch_Diagnostics
|
||||
/// Architecture-specific call-stack tracing routines.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_Diagnostics
|
||||
///@{
|
||||
|
||||
/// Dumps call-stack info to a file, and prints an informative message.
|
||||
///
|
||||
/// The reason for the trace should be supplied in \p reason. This routine
|
||||
/// can be slow and is intended to be called for a fatal error, such as a
|
||||
/// caught coredump signal, but may be called at any time. An additional
|
||||
/// message may be provided in \p message. If \p reason is \c NULL then this
|
||||
/// function only writes \p message to the banner (if any).
|
||||
///
|
||||
/// This function is implemented by calling an external program. This is
|
||||
/// suitable for times where the current process may be corrupted. In other
|
||||
/// cases, using \c ArchPrintStackTrace() or other related functions would be
|
||||
/// much faster.
|
||||
///
|
||||
/// Note the use of \c char* as opposed to \c string: this is intentional,
|
||||
/// because we are trying to use only async-safe function from here on and
|
||||
/// malloc() is not async-safe.
|
||||
ARCH_API
|
||||
void ArchLogPostMortem(
|
||||
const char* reason,
|
||||
const char* message = nullptr,
|
||||
const char* extraLogMsg = nullptr);
|
||||
|
||||
/// Sets the command line that gathers call-stack info.
|
||||
///
|
||||
/// This function sets the command line to execute to gather and log
|
||||
/// call-stack info. \p argv must be NULL terminated. \p command and/or \p
|
||||
/// argv may be NULL to suppress execution. Otherwise argv[0] must be the
|
||||
/// full path to the program to execute, typically \p command or "$cmd" as
|
||||
/// described below.
|
||||
///
|
||||
/// \p command and \p argv are not copied and must remain valid until the next
|
||||
/// call to \c ArchSetPostMortem.
|
||||
///
|
||||
/// Simple substitution is supported on argv elements:
|
||||
/// \li $cmd: Substitutes the command pathname, or $ARCH_POSTMORTEM if set
|
||||
/// \li $pid: Substitutes the process id
|
||||
/// \li $log: Substitutes the log pathname
|
||||
/// \li $time: Substitutes the user time (if available, else wall time)
|
||||
///
|
||||
/// \sa ArchLogPostMortem
|
||||
ARCH_API
|
||||
void ArchSetPostMortem(const char* command, const char *const argv[]);
|
||||
|
||||
/// Log session info.
|
||||
///
|
||||
/// Optionally indicate that this is due to a crash by providing
|
||||
/// the path to a file containing a stack trace in \p crashStackTrace.
|
||||
///
|
||||
ARCH_API
|
||||
void ArchLogSessionInfo(const char *crashStackTrace=NULL);
|
||||
|
||||
/// Sets the command line to log sessions.
|
||||
///
|
||||
/// This function sets the command line to execute to log session info. \p
|
||||
/// argv is used if no crash stack trace is provided, otherwise \p crashArgv
|
||||
/// is used. Both must be NULL terminated. If \p command or \p argv is NULL
|
||||
/// then non-crashes are not logged; if \p command or \p crashArgv is NULL
|
||||
/// then crashes are not logged. If not NULL then argv[0] and crashArgv[0]
|
||||
/// must be full path to the program to execute, typically \p command or
|
||||
/// "$cmd" as described below.
|
||||
///
|
||||
/// \p command, \p argv, and \p crashArgv are not copied and must remain valid
|
||||
/// until the next call to \c ArchSetLogSession.
|
||||
///
|
||||
/// Simple substitution is supported on argv elements:
|
||||
/// \li $cmd: Substitutes the command pathname, or $ARCH_LOGSESSION if set
|
||||
/// \li $prog Substitutes the program name
|
||||
/// \li $pid: Substitutes the process id
|
||||
/// \li $time: Substitutes the user time (if available, else wall time)
|
||||
/// \li $stack: Substitutes the crash stack string (only in crashArgv)
|
||||
///
|
||||
/// \sa ArchLogSessionInfo
|
||||
ARCH_API
|
||||
void ArchSetLogSession(const char* command,
|
||||
const char* const argv[],
|
||||
const char* const crashArgv[]);
|
||||
|
||||
/// Register the callback to invoke logging at end of a successful session.
|
||||
///
|
||||
/// This function registers ArchLogSessionInfo() and records the current
|
||||
/// timestamp, to send up-time to the DB upon exiting.
|
||||
ARCH_API
|
||||
void ArchEnableSessionLogging();
|
||||
|
||||
/// Print a stack trace to the given FILE pointer.
|
||||
ARCH_API
|
||||
void ArchPrintStackTrace(FILE *fout,
|
||||
const std::string& programName,
|
||||
const std::string& reason);
|
||||
|
||||
/// Print a stack trace to the given FILE pointer.
|
||||
/// This function uses ArchGetProgramInfoForErrors as the \c programName.
|
||||
/// \overload
|
||||
ARCH_API
|
||||
void ArchPrintStackTrace(FILE *fout, const std::string& reason);
|
||||
|
||||
/// Print a stack trace to the given ostream.
|
||||
/// \overload
|
||||
ARCH_API
|
||||
void ArchPrintStackTrace(std::ostream& out,
|
||||
const std::string& programName,
|
||||
const std::string& reason);
|
||||
|
||||
/// Print a stack trace to the given ostream.
|
||||
/// This function uses ArchGetProgramInfoForErrors as the \c programName.
|
||||
/// \overload
|
||||
ARCH_API
|
||||
void ArchPrintStackTrace(std::ostream& out, const std::string& reason);
|
||||
|
||||
/// A callback to get a symbolic representation of an address.
|
||||
typedef std::function<std::string(uintptr_t address)> ArchStackTraceCallback;
|
||||
|
||||
/// Sets a callback to get a symbolic representation of an address.
|
||||
///
|
||||
/// The callback returns a string for an address in a stack trace, typically
|
||||
/// including the name of the function containing the address. \p cb may be \c
|
||||
/// NULL to use a default implementation.
|
||||
ARCH_API
|
||||
void ArchSetStackTraceCallback(const ArchStackTraceCallback& cb);
|
||||
|
||||
/// Returns the callback to get a symbolic representation of an address.
|
||||
/// \sa ArchSetStackTraceCallback
|
||||
ARCH_API
|
||||
void ArchGetStackTraceCallback(ArchStackTraceCallback* cb);
|
||||
|
||||
/// Returns the set value for the application's launch time.
|
||||
/// The timestamp for this value is set when the arch library is initialized.
|
||||
ARCH_API
|
||||
time_t ArchGetAppLaunchTime();
|
||||
|
||||
/// Enables or disables the automatic logging of crash information.
|
||||
///
|
||||
/// This function controls whether the stack trace and build information is
|
||||
/// automatically caught and stored to an internal database when a fatal crash
|
||||
/// occurs.
|
||||
ARCH_API
|
||||
void ArchSetFatalStackLogging(bool flag);
|
||||
|
||||
/// Returns whether automatic logging of fatal crashes is enabled
|
||||
/// This is set to false by default.
|
||||
/// \see ArchSetFatalStackLogging
|
||||
ARCH_API
|
||||
bool ArchGetFatalStackLogging();
|
||||
|
||||
/// Sets the program name to be used in diagnostic output
|
||||
///
|
||||
/// The default value is initialized to ArchGetExecutablePath().
|
||||
ARCH_API
|
||||
void ArchSetProgramNameForErrors(const char * progName);
|
||||
|
||||
/// Returns the currently set program name for reporting errors.
|
||||
/// Defaults to ArchGetExecutablePath().
|
||||
ARCH_API
|
||||
const char * ArchGetProgramNameForErrors();
|
||||
|
||||
/// Sets additional program info to be reported to the terminal in case of a
|
||||
/// fatal error.
|
||||
ARCH_API
|
||||
void ArchSetProgramInfoForErrors( const std::string& key, const std::string& value );
|
||||
|
||||
/// Returns currently set program info.
|
||||
/// \see ArchSetExtraLogInfoForErrors
|
||||
ARCH_API
|
||||
std::string ArchGetProgramInfoForErrors(const std::string& key);
|
||||
|
||||
/// Stores (or removes if \p lines is nullptr) a pointer to additional log data
|
||||
/// that will be output in the stack trace log in case of a fatal error. Note
|
||||
/// that the pointer \p lines is copied, not the pointed-to data. In addition,
|
||||
/// Arch might read the data pointed to by \p lines concurrently at any time.
|
||||
/// Thus it is the caller's responsibility to ensure that \p lines is both valid
|
||||
/// and not mutated until replacing or removing it by invoking this function
|
||||
/// again with the same \p key and different \p lines.
|
||||
ARCH_API
|
||||
void ArchSetExtraLogInfoForErrors(const std::string &key,
|
||||
std::vector<std::string> const *lines);
|
||||
|
||||
/// Logs a stack trace to a file in /var/tmp.
|
||||
///
|
||||
/// This function is similar to \c ArchLogPostMortem(), but will not fork an
|
||||
/// external process and only reports a stack trace. A file in /var/tmp is
|
||||
/// created with the name \c st_APPNAME.XXXXXX, where \c mktemp is used to
|
||||
/// make a unique extension for the file. If \c sessionLog is specified, then
|
||||
/// it will be appended to this file. A message is printed to \c stderr
|
||||
/// reporting that a stack trace has been taken and what file it has been
|
||||
/// written to. And if \c fatal is true, then the stack trace will be added
|
||||
/// to the stack_trace database table.
|
||||
ARCH_API
|
||||
void ArchLogStackTrace(const std::string& progName,
|
||||
const std::string& reason,
|
||||
bool fatal = false,
|
||||
const std::string& sessionLog = "");
|
||||
|
||||
/// Logs a stack trace to a file in /var/tmp.
|
||||
///
|
||||
/// This function is similar to \c ArchLogPostMortem(), but will not fork an
|
||||
/// external process and only reports a stack trace. A file in /var/tmp is
|
||||
/// created with the name \c st_APPNAME.XXXXXX, where \c mktemp is used to
|
||||
/// make a unique extension for the file. If \c sessionLog is specified,
|
||||
/// then it will be appended to this file. A message is printed to \c stderr
|
||||
/// reporting that a stack trace has been taken and what file it has been
|
||||
/// written to. And if \c fatal is true, then the stack trace will be added
|
||||
/// to the stack_trace database table.
|
||||
ARCH_API
|
||||
void ArchLogStackTrace(const std::string& reason,
|
||||
bool fatal = false,
|
||||
const std::string& sessionLog = "");
|
||||
|
||||
/// Return stack trace.
|
||||
///
|
||||
/// This function will return a vector of strings containing the current
|
||||
/// stack. The vector will be of maximum size \p maxDepth.
|
||||
ARCH_API
|
||||
std::vector<std::string> ArchGetStackTrace(size_t maxDepth);
|
||||
|
||||
|
||||
/// Save frames of current stack
|
||||
///
|
||||
/// This function saves at maximum \c maxDepth frames of the current stack
|
||||
/// into the vector \c frames.
|
||||
ARCH_API
|
||||
void ArchGetStackFrames(size_t maxDepth, std::vector<uintptr_t> *frames);
|
||||
|
||||
/// Save frames of current stack.
|
||||
///
|
||||
/// This function saves at maximum \p maxDepth frames of the current stack
|
||||
/// into the vector \p frames, skipping the first \p numFramesToSkipAtTop
|
||||
/// frames. The first frame will be at depth \p numFramesToSkipAtTop and the
|
||||
/// last at depth \p numFramesToSkipAtTop + \p maxDepth - 1.
|
||||
ARCH_API
|
||||
void ArchGetStackFrames(size_t maxDepth, size_t numFramesToSkipAtTop,
|
||||
std::vector<uintptr_t> *frames);
|
||||
|
||||
/// Print stack frames to the given ostream.
|
||||
ARCH_API
|
||||
void ArchPrintStackFrames(std::ostream& out,
|
||||
const std::vector<uintptr_t> &frames);
|
||||
|
||||
/// Callback for handling crashes.
|
||||
/// \see ArchCrashHandlerSystemv
|
||||
typedef void (*ArchCrashHandlerSystemCB)(void* userData);
|
||||
|
||||
/// Replacement for 'system' safe for a crash handler
|
||||
///
|
||||
/// This function is a substitute for system() which does not allocate or free
|
||||
/// any data, and times out after \c timeout seconds if the operation in \c
|
||||
/// argv is not complete. Unlike system, it takes the full \c pathname of the
|
||||
/// program to run, and won't search the path. Also unlike system, \c argv[]
|
||||
/// are the separated arguments, starting with the program's name, as for
|
||||
/// execv. \c callback is called every second. \c userData is passed to \c
|
||||
/// callback. \c callback can be used, for example, to print a '.' repeatedly
|
||||
/// to show progress. The alarm used in this function could interfere with
|
||||
/// setitimer or other calls to alarm, and this function uses non-locking fork
|
||||
/// and exec if available so should not generally be used except following a
|
||||
/// catastrophe.
|
||||
ARCH_API
|
||||
int ArchCrashHandlerSystemv(const char* pathname, char *const argv[],
|
||||
int timeout, ArchCrashHandlerSystemCB callback,
|
||||
void* userData);
|
||||
|
||||
/// Crash, to test crash behavior.
|
||||
///
|
||||
/// This function causes the calling program to crash by doing bad malloc
|
||||
/// and free things. If \c spawnthread is true, it spawns a thread which
|
||||
/// remains alive during the crash. It aborts if it fails to crash.
|
||||
///
|
||||
/// \private
|
||||
ARCH_API
|
||||
void ArchTestCrash(bool spawnthread);
|
||||
|
||||
#if defined(ARCH_OS_DARWIN)
|
||||
// macOS has no ETIME. ECANCELED seems to have about the closest meaning to
|
||||
// the actual error here. The operation is timing out, not being explicitly
|
||||
// canceled, but it is canceled.
|
||||
#ifndef ETIME
|
||||
#define ETIME ECANCELED
|
||||
#endif // end ETIME
|
||||
#endif // end ARCH_OS_DARWIN
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_STACK_TRACE_H
|
||||
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_SYMBOLS_H
|
||||
#define PXR_BASE_ARCH_SYMBOLS_H
|
||||
|
||||
/// \file arch/symbols.h
|
||||
/// \ingroup group_arch_Diagnostics
|
||||
/// Architecture-specific symbol lookup routines.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Returns information about the address \p address in the running program.
|
||||
///
|
||||
/// Returns \c false if no information can be found, otherwise returns \c true
|
||||
/// and modifies the other arguments: \p objectPath is set to the absolute path
|
||||
/// to the executable or library the address is found in, \p baseAddress is the
|
||||
/// address where that object is loaded, \p symbolName is the symbolic name of
|
||||
/// the thing containing the address, and \p symbolAddress is the starting
|
||||
/// address of that thing. If no thing is found to contain the address then
|
||||
/// \p symbolName is cleared and \p symbolAddress is set to \c NULL. Any of
|
||||
/// the arguments except \p address can be \c NULL if the result isn't needed.
|
||||
/// This will return \c false if \c NULL is passed to \p address.
|
||||
///
|
||||
/// \ingroup group_arch_Diagnostics
|
||||
ARCH_API
|
||||
bool ArchGetAddressInfo(void* address,
|
||||
std::string* objectPath, void** baseAddress,
|
||||
std::string* symbolName, void** symbolAddress);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_SYMBOLS_H
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_SYSTEM_INFO_H
|
||||
#define PXR_BASE_ARCH_SYSTEM_INFO_H
|
||||
|
||||
/// \file arch/systemInfo.h
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
/// Provide architecture-specific system information.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_SystemFunctions
|
||||
///@{
|
||||
|
||||
/// Return current working directory as a string.
|
||||
ARCH_API
|
||||
std::string ArchGetCwd();
|
||||
|
||||
/// Return the path to the program's executable.
|
||||
ARCH_API
|
||||
std::string ArchGetExecutablePath();
|
||||
|
||||
/// Return the system's memory page size. Safe to assume power-of-two.
|
||||
ARCH_API
|
||||
int ArchGetPageSize();
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_SYSTEM_INFO_H
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_THREADS_H
|
||||
#define PXR_BASE_ARCH_THREADS_H
|
||||
|
||||
/// \file arch/threads.h
|
||||
/// \ingroup group_arch_Multithreading
|
||||
/// Architecture-specific thread function calls.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Return true if the calling thread is the main thread, false otherwise.
|
||||
/// \ingroup group_arch_Multithreading
|
||||
ARCH_API bool ArchIsMainThread();
|
||||
|
||||
/// Return the std::thread_id for the thread arch considers to be the "main"
|
||||
/// thread.
|
||||
ARCH_API std::thread::id ArchGetMainThreadId();
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_THREADS_H
|
||||
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_TIMING_H
|
||||
#define PXR_BASE_ARCH_TIMING_H
|
||||
|
||||
/// \file arch/timing.h
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
/// High-resolution, low-cost timing routines.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/defines.h"
|
||||
#include "pxr/base/arch/inttypes.h"
|
||||
|
||||
/// \addtogroup group_arch_SystemFunctions
|
||||
///@{
|
||||
|
||||
#if defined(ARCH_OS_LINUX)
|
||||
#include <x86intrin.h>
|
||||
#elif defined(ARCH_OS_DARWIN)
|
||||
#include <mach/mach_time.h>
|
||||
#elif defined(ARCH_OS_WINDOWS)
|
||||
#include <intrin.h>
|
||||
#endif
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Macro that defines the clocks per second
|
||||
///
|
||||
/// Unfortunately, Red Hat 7.1 does not define CLK_TCK correctly, so
|
||||
/// ARCH_CLK_TCK is the only guaranteed way to get the value of CLK_TCK. The
|
||||
/// value is currently the same on all of our platforms.
|
||||
#define ARCH_CLK_TCK 100
|
||||
|
||||
/// Return the current time in system-dependent units.
|
||||
///
|
||||
/// The current time is returned as a number of "ticks", where each tick
|
||||
/// represents some system-dependent amount of time. The resolution of the
|
||||
/// timing routines varies, but on all systems, it is well under one
|
||||
/// microsecond. The cost of this routine is in the tens of nanoseconds
|
||||
/// on GHz class machines.
|
||||
inline uint64_t
|
||||
ArchGetTickTime()
|
||||
{
|
||||
#if defined(ARCH_OS_DARWIN)
|
||||
// On Darwin we'll use mach_absolute_time().
|
||||
return mach_absolute_time();
|
||||
#elif defined(ARCH_CPU_INTEL)
|
||||
// On Intel we'll use the rdtsc instruction.
|
||||
return __rdtsc();
|
||||
#else
|
||||
#error Unknown architecture.
|
||||
#endif
|
||||
}
|
||||
|
||||
/// Convert a duration measured in "ticks", as returned by
|
||||
/// \c ArchGetTickTime(), to nanoseconds.
|
||||
///
|
||||
/// An example to test the timing routines would be:
|
||||
/// \code
|
||||
/// int64_t t1 = ArchGetTickTime();
|
||||
/// sleep(10);
|
||||
/// int64_t t2 = ArchGetTickTime();
|
||||
///
|
||||
/// // duration should be approximately 10/// 1e9 = 1e10 nanoseconds.
|
||||
/// int64_t duration = ArchTicksToNanoseconds(t2 - t1);
|
||||
/// \endcode
|
||||
///
|
||||
ARCH_API
|
||||
int64_t ArchTicksToNanoseconds(uint64_t nTicks);
|
||||
|
||||
/// Convert a duration measured in "ticks", as returned by
|
||||
/// \c ArchGetTickTime(), to seconds.
|
||||
ARCH_API
|
||||
double ArchTicksToSeconds(uint64_t nTicks);
|
||||
|
||||
/// Convert a duration in seconds to "ticks", as returned by
|
||||
/// \c ArchGetTickTime().
|
||||
ARCH_API
|
||||
uint64_t ArchSecondsToTicks(double seconds);
|
||||
|
||||
/// Get nanoseconds per tick. Useful when converting ticks obtained from
|
||||
/// \c ArchTickTime()
|
||||
ARCH_API
|
||||
double ArchGetNanosecondsPerTick();
|
||||
|
||||
///@}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_TIMING_H
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Copyright 2019 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_VIRTUAL_MEMORY_H
|
||||
#define PXR_BASE_ARCH_VIRTUAL_MEMORY_H
|
||||
|
||||
/// \file arch/virtualMemory.h
|
||||
/// \ingroup group_arch_SystemFunctions
|
||||
/// Architecture dependent routines for virtual memory.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Reserve \p numBytes bytes of virtual memory. Call ArchCommitVirtualMemory()
|
||||
/// on subranges to write to and read from the memory. Return nullptr in case
|
||||
/// of an error; check errno.
|
||||
ARCH_API void *
|
||||
ArchReserveVirtualMemory(size_t numBytes);
|
||||
|
||||
/// Make the range of \p numBytes bytes starting at \p start available for
|
||||
/// reading and writing. The range must be within one previously reserved by
|
||||
/// ArchReserveVirtualMemory(). It is not an error to commit a range that was
|
||||
/// previously partly or fully committed. Return false in case of an error;
|
||||
/// check errno.
|
||||
ARCH_API bool
|
||||
ArchCommitVirtualMemoryRange(void *start, size_t numBytes);
|
||||
|
||||
/// Return memory obtained with ArchReserveVirtualMemory() to the system. The
|
||||
/// \p start argument must be the value returned from a previous call to
|
||||
/// ArchReserveVirtualMemory, and \p numBytes must match the argument from that
|
||||
/// call. Memory within the range may not be accessed after this call. Return
|
||||
/// false in case of an error; check errno.
|
||||
ARCH_API bool
|
||||
ArchFreeVirtualMemory(void *start, size_t numBytes);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_VIRTUAL_MEMORY_H
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_ARCH_VSNPRINTF_H
|
||||
#define PXR_BASE_ARCH_VSNPRINTF_H
|
||||
|
||||
/// \file arch/vsnprintf.h
|
||||
/// \ingroup group_arch_Strings
|
||||
/// Architecture dependent memory-safe sprintf capability
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/api.h"
|
||||
#include "pxr/base/arch/attributes.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \addtogroup group_arch_Strings
|
||||
///@{
|
||||
|
||||
/// Return the number of characters (not including the null character)
|
||||
/// necessary for a particular sprintf into a string.
|
||||
///
|
||||
/// \c ArchVsnprintf guarantees the C99 behavior of vsnprintf on all systems:
|
||||
/// it returns the number of bytes (not including the terminating null
|
||||
/// character) needed to actually print the requested string. If \c size
|
||||
/// indicates that \c str has enough capacity to hold the result, then the
|
||||
/// function actually prints into \c str.
|
||||
///
|
||||
/// You probably want to use the functionality of this call via
|
||||
/// \c TfStringPrintf().
|
||||
///
|
||||
ARCH_API
|
||||
int ArchVsnprintf(char *str, size_t size, const char *format, va_list ap)
|
||||
#ifndef doxygen
|
||||
ARCH_PRINTF_FUNCTION(3, 0)
|
||||
#endif /* doxygen */
|
||||
;
|
||||
|
||||
/// Returns a string formed by a printf()-like specification.
|
||||
///
|
||||
/// \c ArchStringPrintf() is a memory-safe architecture-independent way of
|
||||
/// forming a string using printf()-like formatting. For example,
|
||||
/// \code
|
||||
/// string formatMsg(const string& caller, int i, double val[])
|
||||
/// {
|
||||
/// return ArchStringPrintf("%s: val[%d] = %g\n", caller.c_str(), i, val[i]);
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// The function is safe only to the extent that the arguments match the
|
||||
/// formatting string. In particular, be careful to pass strings themselve
|
||||
/// into \c ArchStringPrintf() as in the above example (i.e. \c caller.c_str()
|
||||
/// as opposed to just passing \c caller).
|
||||
///
|
||||
ARCH_API
|
||||
std::string ArchStringPrintf(const char *fmt, ...)
|
||||
#ifndef doxygen
|
||||
ARCH_PRINTF_FUNCTION(1, 2)
|
||||
#endif /* doxygen */
|
||||
;
|
||||
|
||||
/// Returns a string formed by a printf()-like specification.
|
||||
///
|
||||
/// \c ArchVStringPrintf() is equivalent to \c ArchStringPrintf() except that
|
||||
/// it is called with a \c va_list instead of a variable number of arguments.
|
||||
/// \c ArchVStringPrintf() does not call the \c va_end macro. Consequently,
|
||||
/// the value of \c ap is undefined after the call. A functions that calls \c
|
||||
/// ArchVStringPrintf() should call \c va_end(ap) itself afterwards.
|
||||
///
|
||||
ARCH_API
|
||||
std::string ArchVStringPrintf(const char *fmt, va_list ap)
|
||||
#ifndef doxygen
|
||||
ARCH_PRINTF_FUNCTION(1, 0)
|
||||
;
|
||||
#endif
|
||||
|
||||
/// @}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_ARCH_VSNPRINTF_H
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2017 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_API_H
|
||||
#define PXR_BASE_GF_API_H
|
||||
|
||||
#include "pxr/base/arch/export.h"
|
||||
|
||||
#if defined(PXR_STATIC)
|
||||
# define GF_API
|
||||
# define GF_API_TEMPLATE_CLASS(...)
|
||||
# define GF_API_TEMPLATE_STRUCT(...)
|
||||
# define GF_LOCAL
|
||||
#else
|
||||
# if defined(GF_EXPORTS)
|
||||
# define GF_API ARCH_EXPORT
|
||||
# define GF_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define GF_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# else
|
||||
# define GF_API ARCH_IMPORT
|
||||
# define GF_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define GF_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# endif
|
||||
# define GF_LOCAL ARCH_HIDDEN
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,269 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_BBOX3D_H
|
||||
#define PXR_BASE_GF_BBOX3D_H
|
||||
|
||||
/// \file gf/bbox3d.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/matrix4d.h"
|
||||
#include "pxr/base/gf/range3d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfBBox3d
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
/// Basic type: arbitrarily oriented 3D bounding box.
|
||||
///
|
||||
/// This class represents a three-dimensional bounding box as an
|
||||
/// axis-aligned box (\c GfRange3d) and a matrix (\c GfMatrix4d) to
|
||||
/// transform it into the correct space.
|
||||
///
|
||||
/// A \c GfBBox3d is more useful than using just \c GfRange3d instances
|
||||
/// (which are always axis-aligned) for these reasons:
|
||||
///
|
||||
/// \li When an axis-aligned bounding box is transformed several times,
|
||||
/// each transformation can result in inordinate growth of the bounding
|
||||
/// box. By storing the transformation separately, it can be applied once
|
||||
/// at the end, resulting in a much better fit. For example, if the
|
||||
/// bounding box at the leaf of a scene graph is transformed through
|
||||
/// several levels of the graph hierarchy to the coordinate space at the
|
||||
/// root, a \c GfBBox3d is generally much smaller than the \c GfRange3d
|
||||
/// computed by transforming the box at each level.
|
||||
///
|
||||
/// \li When two or more such bounding boxes are combined, having the
|
||||
/// transformations stored separately means that there is a better
|
||||
/// opportunity to choose a better coordinate space in which to combine
|
||||
/// the boxes.
|
||||
///
|
||||
/// \anchor bbox3d_zeroAreaFlag
|
||||
/// <b> The Zero-area Primitives Flag </b>
|
||||
///
|
||||
/// When bounding boxes are used in intersection test culling, it is
|
||||
/// sometimes useful to extend them a little bit to allow
|
||||
/// lower-dimensional objects with zero area, such as lines and points,
|
||||
/// to be intersected. For example, consider a cube constructed of line
|
||||
/// segments. The bounding box for this shape fits the cube exactly. If
|
||||
/// an application wants to allow a near-miss of the silhouette edges of
|
||||
/// the cube to be considered an intersection, it has to loosen the bbox
|
||||
/// culling test a little bit.
|
||||
///
|
||||
/// To distinguish when this loosening is necessary, each \c GfBBox3d
|
||||
/// instance maintains a flag indicating whether any zero-area primitives
|
||||
/// are contained within it. The application is responsible for setting
|
||||
/// this flag correctly by calling \c SetHasZeroAreaPrimitives(). The
|
||||
/// flag can be accessed during intersection tests by calling \c
|
||||
/// HasZeroAreaPrimitives(). This flag is set by default in all
|
||||
/// constructors to \c false.
|
||||
///
|
||||
class GfBBox3d {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves the box empty, the transformation
|
||||
/// matrix identity, and the \ref bbox3d_zeroAreaFlag "zero-area
|
||||
/// primitives flag" \c false.
|
||||
GfBBox3d() {
|
||||
_matrix.SetIdentity();
|
||||
_inverse.SetIdentity();
|
||||
_isDegenerate = false;
|
||||
_hasZeroAreaPrimitives = false;
|
||||
}
|
||||
|
||||
/// Copy constructor
|
||||
GfBBox3d(const GfBBox3d& rhs) :
|
||||
_box(rhs._box) {
|
||||
_matrix = rhs._matrix;
|
||||
_inverse = rhs._inverse;
|
||||
_isDegenerate = rhs._isDegenerate;
|
||||
_hasZeroAreaPrimitives = rhs._hasZeroAreaPrimitives;
|
||||
}
|
||||
|
||||
/// This constructor takes a box and sets the matrix to identity.
|
||||
GfBBox3d(const GfRange3d &box) :
|
||||
_box(box) {
|
||||
_matrix.SetIdentity();
|
||||
_inverse.SetIdentity();
|
||||
_isDegenerate = false;
|
||||
_hasZeroAreaPrimitives = false;
|
||||
}
|
||||
|
||||
/// This constructor takes a box and a transformation matrix.
|
||||
GfBBox3d(const GfRange3d &box, const GfMatrix4d &matrix) {
|
||||
Set(box, matrix);
|
||||
_hasZeroAreaPrimitives = false;
|
||||
}
|
||||
|
||||
/// Sets the axis-aligned box and transformation matrix.
|
||||
void Set(const GfRange3d &box, const GfMatrix4d &matrix) {
|
||||
_box = box;
|
||||
_SetMatrices(matrix);
|
||||
}
|
||||
|
||||
/// Sets the transformation matrix only. The axis-aligned box is not
|
||||
/// modified.
|
||||
void SetMatrix(const GfMatrix4d& matrix) {
|
||||
_SetMatrices(matrix);
|
||||
}
|
||||
|
||||
/// Sets the range of the axis-aligned box only. The transformation
|
||||
/// matrix is not modified.
|
||||
void SetRange(const GfRange3d& box) {
|
||||
_box = box;
|
||||
}
|
||||
|
||||
/// Returns the range of the axis-aligned untransformed box.
|
||||
const GfRange3d & GetRange() const {
|
||||
return _box;
|
||||
}
|
||||
|
||||
/// Returns the range of the axis-aligned untransformed box.
|
||||
/// This synonym of \c GetRange exists for compatibility purposes.
|
||||
const GfRange3d & GetBox() const {
|
||||
return GetRange();
|
||||
}
|
||||
|
||||
/// Returns the transformation matrix.
|
||||
const GfMatrix4d & GetMatrix() const {
|
||||
return _matrix;
|
||||
}
|
||||
|
||||
/// Returns the inverse of the transformation matrix. This will be the
|
||||
/// identity matrix if the transformation matrix is not invertible.
|
||||
const GfMatrix4d & GetInverseMatrix() const {
|
||||
return _inverse;
|
||||
}
|
||||
|
||||
/// Sets the \ref bbox3d_zeroAreaFlag "zero-area primitives flag" to the
|
||||
/// given value.
|
||||
void SetHasZeroAreaPrimitives(bool hasThem) {
|
||||
_hasZeroAreaPrimitives = hasThem;
|
||||
}
|
||||
|
||||
/// Returns the current state of the \ref bbox3d_zeroAreaFlag "zero-area
|
||||
/// primitives flag".
|
||||
bool HasZeroAreaPrimitives() const {
|
||||
return _hasZeroAreaPrimitives;
|
||||
}
|
||||
|
||||
/// Returns the volume of the box (0 for an empty box).
|
||||
GF_API
|
||||
double GetVolume() const;
|
||||
|
||||
/// Transforms the bounding box by the given matrix, which is assumed to
|
||||
/// be a global transformation to apply to the box. Therefore, this just
|
||||
/// post-multiplies the box's matrix by \p matrix.
|
||||
void Transform(const GfMatrix4d &matrix) {
|
||||
_SetMatrices(_matrix * matrix);
|
||||
}
|
||||
|
||||
/// Returns the axis-aligned range (as a \c GfRange3d) that results from
|
||||
/// applying the transformation matrix to the wxis-aligned box and
|
||||
/// aligning the result.
|
||||
GF_API
|
||||
GfRange3d ComputeAlignedRange() const;
|
||||
|
||||
/// Returns the axis-aligned range (as a \c GfRange3d) that results from
|
||||
/// applying the transformation matrix to the axis-aligned box and
|
||||
/// aligning the result. This synonym for \c ComputeAlignedRange exists
|
||||
/// for compatibility purposes.
|
||||
GfRange3d ComputeAlignedBox() const {
|
||||
return ComputeAlignedRange();
|
||||
}
|
||||
|
||||
/// Combines two bboxes, returning a new bbox that contains both. This
|
||||
/// uses the coordinate space of one of the two original boxes as the
|
||||
/// space of the result; it uses the one that produces whe smaller of the
|
||||
/// two resulting boxes.
|
||||
GF_API
|
||||
static GfBBox3d Combine(const GfBBox3d &b1, const GfBBox3d &b2);
|
||||
|
||||
/// Returns the centroid of the bounding box.
|
||||
/// The centroid is computed as the transformed centroid of the range.
|
||||
GF_API
|
||||
GfVec3d ComputeCentroid() const;
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(const GfBBox3d &b) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, b._box);
|
||||
boost::hash_combine(h, b._matrix);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Component-wise equality test. The axis-aligned boxes and
|
||||
/// transformation matrices match exactly for bboxes to be considered
|
||||
/// equal. (To compare equality of the actual boxes, you can compute both
|
||||
/// aligned boxes and test the results for equality.)
|
||||
bool operator ==(const GfBBox3d &b) const {
|
||||
return (_box == b._box &&
|
||||
_matrix == b._matrix);
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The axis-aligned boxes and
|
||||
/// transformation matrices match exactly for bboxes to be considered
|
||||
/// equal. (To compare equality of the actual boxes, you can compute both
|
||||
/// aligned boxes and test the results for equality.)
|
||||
bool operator !=(const GfBBox3d &that) const {
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
private:
|
||||
/// The axis-aligned box.
|
||||
GfRange3d _box;
|
||||
/// Transformation matrix.
|
||||
GfMatrix4d _matrix;
|
||||
/// Inverse of the transformation matrix.
|
||||
GfMatrix4d _inverse;
|
||||
/// Flag indicating whether the matrix is degenerate.
|
||||
bool _isDegenerate;
|
||||
/// Flag indicating whether the bbox contains zero-area primitives.
|
||||
bool _hasZeroAreaPrimitives;
|
||||
|
||||
/// Sets the transformation matrix and the inverse, checking for
|
||||
/// degeneracies.
|
||||
GF_API
|
||||
void _SetMatrices(const GfMatrix4d &matrix);
|
||||
|
||||
/// This is used by \c Combine() when it is determined which coordinate
|
||||
/// space to use to combine two boxes: \p b2 is transformed into the space
|
||||
/// of \p b1 and the results are merged in that space.
|
||||
static GfBBox3d _CombineInOrder(const GfBBox3d &b1, const GfBBox3d &b2);
|
||||
};
|
||||
|
||||
/// Output a GfBBox3d using the format [(range) matrix zeroArea]
|
||||
///
|
||||
/// The zeroArea flag is true or false and indicates whether the
|
||||
/// bbox has zero area primitives in it.
|
||||
///
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream&, const GfBBox3d&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_BBOX3D_H
|
||||
@@ -0,0 +1,256 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_CAMERA_H
|
||||
#define PXR_BASE_GF_CAMERA_H
|
||||
|
||||
/// \file gf/camera.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/matrix4d.h"
|
||||
#include "pxr/base/gf/range1f.h"
|
||||
#include "pxr/base/gf/vec4f.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfFrustum;
|
||||
|
||||
/// \class GfCamera
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
/// \brief Object-based representation of a camera.
|
||||
///
|
||||
/// This class provides a thin wrapper on the camera data model,
|
||||
/// with a small number of computations.
|
||||
///
|
||||
class GfCamera
|
||||
{
|
||||
public:
|
||||
/// Projection type.
|
||||
enum Projection {
|
||||
Perspective = 0,
|
||||
Orthographic,
|
||||
};
|
||||
|
||||
/// Direction used for Field of View or orthographic size
|
||||
enum FOVDirection {
|
||||
FOVHorizontal = 0,
|
||||
FOVVertical
|
||||
};
|
||||
|
||||
/// The unit for horizontal and vertical aperture is one tenth of the
|
||||
/// world unit. Thus, if the world unit is assumed to be cm, the horizontal
|
||||
/// and vertical aperture unit is mm.
|
||||
GF_API static const double APERTURE_UNIT;
|
||||
/// The unit for focal length. Similar to APERTURE_UNIT.
|
||||
GF_API static const double FOCAL_LENGTH_UNIT;
|
||||
|
||||
/// Default horizontal and vertical aperture, based on a 35mm
|
||||
/// (non-anamorphic) projector aperture (0.825 x 0602 inches, converted to
|
||||
/// mm).
|
||||
GF_API static const double DEFAULT_HORIZONTAL_APERTURE;
|
||||
GF_API static const double DEFAULT_VERTICAL_APERTURE;
|
||||
|
||||
public:
|
||||
GF_API GfCamera(
|
||||
const GfMatrix4d &transform = GfMatrix4d(1.0),
|
||||
Projection projection = Perspective,
|
||||
float horizontalAperture = DEFAULT_HORIZONTAL_APERTURE,
|
||||
float verticalAperture = DEFAULT_VERTICAL_APERTURE,
|
||||
float horizontalApertureOffset = 0.0,
|
||||
float verticalApertureOffset = 0.0,
|
||||
float focalLength = 50.0,
|
||||
const GfRange1f &clippingRange = GfRange1f(1, 1000000),
|
||||
const std::vector<GfVec4f> &clippingPlanes = std::vector<GfVec4f>(),
|
||||
float fStop = 0.0,
|
||||
float focusDistance = 0.0);
|
||||
|
||||
/// Sets the transform of the filmback in world space to \p val.
|
||||
GF_API void SetTransform(const GfMatrix4d &val);
|
||||
|
||||
/// Sets the projection type.
|
||||
GF_API void SetProjection(const Projection &val);
|
||||
|
||||
/// \name Physics based camera setup
|
||||
|
||||
/// These are the values actually stored in the class and they correspond
|
||||
/// to measurements of an actual physical camera (in mm).
|
||||
/// Together with the clipping range, they determine the camera frustum.
|
||||
|
||||
/// @{
|
||||
|
||||
/// Sets the focal length in tenths of a world unit (e.g., mm if the world
|
||||
/// unit is assumed to be cm).
|
||||
GF_API void SetFocalLength(const float val);
|
||||
|
||||
/// Sets the width of the projector aperture in tenths of a world unit
|
||||
/// (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API void SetHorizontalAperture(const float val);
|
||||
|
||||
/// Sets the height of the projector aperture in tenths of a world unit
|
||||
/// (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API void SetVerticalAperture(const float val);
|
||||
|
||||
/// Sets the horizontal offset of the projector aperture in tenths of a
|
||||
/// world unit (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API void SetHorizontalApertureOffset(const float val);
|
||||
|
||||
/// Sets the vertical offset of the projector aperture in tenths of a
|
||||
/// world unit (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API void SetVerticalApertureOffset(const float val);
|
||||
/// @}
|
||||
|
||||
/// \name Frustum geometry setup
|
||||
/// @{
|
||||
|
||||
/// Sets the frustum to be projective with the given \p aspectRatio
|
||||
/// and horizontal, respectively, vertical field of view \p fieldOfView
|
||||
/// (similar to gluPerspective when direction = FOVVertical).
|
||||
///
|
||||
/// Do not pass values for \p horionztalAperture unless you care about
|
||||
/// DepthOfField.
|
||||
|
||||
GF_API void SetPerspectiveFromAspectRatioAndFieldOfView(
|
||||
float aspectRatio,
|
||||
float fieldOfView,
|
||||
FOVDirection direction,
|
||||
float horizontalAperture = DEFAULT_HORIZONTAL_APERTURE);
|
||||
|
||||
/// Sets the frustum to be orthographic such that it has the given
|
||||
/// \p aspectRatio and such that the orthographic width, respectively,
|
||||
/// orthographic height (in cm) is equal to \p orthographicSize
|
||||
/// (depending on direction).
|
||||
|
||||
GF_API void SetOrthographicFromAspectRatioAndSize(
|
||||
float aspectRatio, float orthographicSize, FOVDirection direction);
|
||||
|
||||
/// Sets the camera from a view and projection matrix.
|
||||
///
|
||||
/// Note that the projection matrix does only determine the ratio
|
||||
/// of aperture to focal length, so there is a choice which defaults
|
||||
/// to 50mm (or more accurately, 50 tenths of a world unit).
|
||||
|
||||
GF_API void SetFromViewAndProjectionMatrix(
|
||||
const GfMatrix4d &viewMatrix, const GfMatrix4d &projMatix,
|
||||
const float focalLength = 50.0);
|
||||
|
||||
/// @}
|
||||
|
||||
/// Sets the clipping range in world units.
|
||||
GF_API void SetClippingRange(const GfRange1f &val);
|
||||
|
||||
/// Sets additional arbitrarily oriented clipping planes.
|
||||
/// A vector (a,b,c,d) encodes a clipping plane that clips off points
|
||||
/// (x,y,z) with
|
||||
///
|
||||
/// a * x + b * y + c * z + d * 1 < 0
|
||||
///
|
||||
/// where (x,y,z) are the coordinates in the camera's space.
|
||||
GF_API void SetClippingPlanes(const std::vector<GfVec4f> &val);
|
||||
|
||||
/// Sets the lens aperture, unitless.
|
||||
GF_API void SetFStop(const float val);
|
||||
|
||||
/// Sets the focus distance in world units.
|
||||
GF_API void SetFocusDistance(const float val);
|
||||
|
||||
/// Returns the transform of the filmback in world space. This is
|
||||
/// exactly the transform specified via SetTransform().
|
||||
GF_API GfMatrix4d GetTransform() const;
|
||||
|
||||
/// Returns the projection type.
|
||||
GF_API Projection GetProjection() const;
|
||||
|
||||
/// Returns the width of the projector aperture in tenths of a world unit
|
||||
/// (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API float GetHorizontalAperture() const;
|
||||
|
||||
/// Returns the height of the projector aperture in tenths of a world unit
|
||||
/// (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API float GetVerticalAperture() const;
|
||||
|
||||
/// Returns the horizontal offset of the projector aperture in tenths of a
|
||||
/// world unit (e.g., mm if the world unit is assumed to be cm).
|
||||
/// In particular, an offset is necessary when writing out a stereo camera
|
||||
/// with finite convergence distance as two cameras.
|
||||
GF_API float GetHorizontalApertureOffset() const;
|
||||
|
||||
/// Returns the vertical offset of the projector aperture in tenths of a
|
||||
/// world unit (e.g., mm if the world unit is assumed to be cm).
|
||||
GF_API float GetVerticalApertureOffset() const;
|
||||
|
||||
/// Returns the projector aperture aspect ratio.
|
||||
GF_API float GetAspectRatio() const;
|
||||
|
||||
/// Returns the focal length in tenths of a world unit (e.g., mm if the
|
||||
/// world unit is assumed to be cm).
|
||||
GF_API float GetFocalLength() const;
|
||||
|
||||
/// Returns the horizontal or vertical field of view in degrees.
|
||||
GF_API float GetFieldOfView(FOVDirection direction) const;
|
||||
|
||||
/// Returns the clipping range in world units.
|
||||
GF_API GfRange1f GetClippingRange() const;
|
||||
|
||||
/// Returns additional clipping planes.
|
||||
GF_API const std::vector<GfVec4f> &GetClippingPlanes() const;
|
||||
|
||||
/// Returns the computed, world-space camera frustum. The frustum
|
||||
/// will always be that of a Y-up, -Z-looking camera.
|
||||
GF_API GfFrustum GetFrustum() const;
|
||||
|
||||
/// Returns the lens aperture.
|
||||
GF_API float GetFStop() const;
|
||||
|
||||
/// Returns the focus distance in world units.
|
||||
GF_API float GetFocusDistance() const;
|
||||
|
||||
/// Equality operator. true iff all parts match.
|
||||
GF_API bool operator==(const GfCamera& other) const;
|
||||
|
||||
// Inequality operator. true iff not equality.
|
||||
GF_API bool operator!=(const GfCamera& other) const;
|
||||
|
||||
private:
|
||||
// frustum
|
||||
GfMatrix4d _transform;
|
||||
Projection _projection;
|
||||
float _horizontalAperture;
|
||||
float _verticalAperture;
|
||||
float _horizontalApertureOffset;
|
||||
float _verticalApertureOffset;
|
||||
float _focalLength;
|
||||
GfRange1f _clippingRange;
|
||||
std::vector<GfVec4f> _clippingPlanes;
|
||||
|
||||
// focus
|
||||
float _fStop;
|
||||
float _focusDistance;
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_CAMERA_H
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_DECLARE_H
|
||||
#define PXR_BASE_GF_DECLARE_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
/// \file gf/declare.h
|
||||
/// Declares Gf types.
|
||||
///
|
||||
/// This file provides forward declarations for Gf types.
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfBBox3d;
|
||||
class GfFrustum;
|
||||
class GfInterval;
|
||||
class GfMultiInterval;
|
||||
class GfLine;
|
||||
class GfLineSeg;
|
||||
class GfPlane;
|
||||
class GfQuatd;
|
||||
class GfQuatf;
|
||||
class GfQuath;
|
||||
class GfQuaternion;
|
||||
class GfRay;
|
||||
class GfRect2i;
|
||||
class GfRect2i;
|
||||
class GfRotation;
|
||||
class GfSize2;
|
||||
class GfSize3;
|
||||
class GfMatrix2d;
|
||||
class GfMatrix2f;
|
||||
class GfMatrix3d;
|
||||
class GfMatrix3f;
|
||||
class GfMatrix4d;
|
||||
class GfMatrix4f;
|
||||
class GfRange1d;
|
||||
class GfRange1f;
|
||||
class GfRange2d;
|
||||
class GfRange2f;
|
||||
class GfRange3d;
|
||||
class GfRange3f;
|
||||
class GfVec2d;
|
||||
class GfVec2f;
|
||||
class GfVec2h;
|
||||
class GfVec2i;
|
||||
class GfVec3d;
|
||||
class GfVec3f;
|
||||
class GfVec3h;
|
||||
class GfVec3i;
|
||||
class GfVec4d;
|
||||
class GfVec4f;
|
||||
class GfVec4h;
|
||||
class GfVec4i;
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_DECLARE_H
|
||||
@@ -0,0 +1,690 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_FRUSTUM_H
|
||||
#define PXR_BASE_GF_FRUSTUM_H
|
||||
|
||||
/// \file gf/frustum.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/bbox3d.h"
|
||||
#include "pxr/base/gf/matrix4d.h"
|
||||
#include "pxr/base/gf/plane.h"
|
||||
#include "pxr/base/gf/ray.h"
|
||||
#include "pxr/base/gf/range1d.h"
|
||||
#include "pxr/base/gf/range2d.h"
|
||||
#include "pxr/base/gf/rotation.h"
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfFrustum
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: View frustum.
|
||||
///
|
||||
/// This class represents a viewing frustum in three dimensional eye space. It
|
||||
/// may represent either a parallel (orthographic) or perspective projection.
|
||||
/// One can think of the frustum as being defined by 6 boundary planes.
|
||||
///
|
||||
/// The frustum is specified using these parameters:
|
||||
/// \li The \em position of the viewpoint.
|
||||
/// \li The \em rotation applied to the default view frame, which is
|
||||
/// looking along the -z axis with the +y axis as the "up"
|
||||
/// direction.
|
||||
/// \li The 2D \em window on the reference plane that defines the left,
|
||||
/// right, top, and bottom planes of the viewing frustum, as
|
||||
/// described below.
|
||||
/// \li The distances to the \em near and \em far planes.
|
||||
/// \li The \em projection \em type
|
||||
/// \li The view distance.
|
||||
///
|
||||
/// The window and near/far parameters combine to define the view frustum as
|
||||
/// follows. Transform the -z axis and the +y axis by the frustum rotation to
|
||||
/// get the world-space \em view \em direction and \em up \em direction. Now
|
||||
/// consider the \em reference \em plane that is perpendicular to the view
|
||||
/// direction, a distance of referencePlaneDepth from the viewpoint, and whose
|
||||
/// y axis corresponds to the up direction. The window rectangle is specified
|
||||
/// in a 2D coordinate system embedded in this plane. The origin of the
|
||||
/// coordinate system is the point at which the view direction vector
|
||||
/// intersects the plane. Therefore, the point (0,1) in this plane is found by
|
||||
/// moving 1 unit along the up direction vector in this plane. The vector from
|
||||
/// the viewpoint to the resulting point will form a 45-degree angle with the
|
||||
/// view direction.
|
||||
///
|
||||
/// The view distance is only useful for interactive applications. It can be
|
||||
/// used to compute a look at point which is useful when rotating around an
|
||||
/// object of interest.
|
||||
///
|
||||
class GfFrustum {
|
||||
public:
|
||||
/// This enum is used to determine the type of projection represented by a
|
||||
/// frustum.
|
||||
enum ProjectionType {
|
||||
Orthographic, ///< Orthographic projection
|
||||
Perspective, ///< Perspective projection
|
||||
};
|
||||
|
||||
/// This constructor creates an instance with default viewing parameters:
|
||||
/// \li The position is the origin.
|
||||
/// \li The rotation is the identity rotation. (The view is along
|
||||
/// the -z axis, with the +y axis as "up").
|
||||
/// \li The window is -1 to +1 in both dimensions.
|
||||
/// \li The near/far interval is (1, 10).
|
||||
/// \li The view distance is 5.0.
|
||||
/// \li The projection type is \c GfFrustum::Perspective.
|
||||
GF_API GfFrustum();
|
||||
|
||||
/// Copy constructor.
|
||||
GfFrustum(GfFrustum const &o)
|
||||
: _position(o._position)
|
||||
, _rotation(o._rotation)
|
||||
, _window(o._window)
|
||||
, _nearFar(o._nearFar)
|
||||
, _viewDistance(o._viewDistance)
|
||||
, _projectionType(o._projectionType)
|
||||
, _planes(nullptr) {
|
||||
if (auto *planes = o._planes.load()) {
|
||||
_planes = new std::array<GfPlane, 6>(*planes);
|
||||
}
|
||||
}
|
||||
|
||||
/// Move constructor.
|
||||
GfFrustum(GfFrustum &&o) noexcept
|
||||
: _position(o._position)
|
||||
, _rotation(o._rotation)
|
||||
, _window(o._window)
|
||||
, _nearFar(o._nearFar)
|
||||
, _viewDistance(o._viewDistance)
|
||||
, _projectionType(o._projectionType)
|
||||
, _planes(nullptr) {
|
||||
if (auto *planes =
|
||||
o._planes.exchange(nullptr, std::memory_order_relaxed)) {
|
||||
_planes = planes;
|
||||
}
|
||||
}
|
||||
|
||||
/// This constructor creates an instance with the given viewing
|
||||
/// parameters.
|
||||
GF_API GfFrustum(const GfVec3d &position, const GfRotation &rotation,
|
||||
const GfRange2d &window, const GfRange1d &nearFar,
|
||||
GfFrustum::ProjectionType projectionType,
|
||||
double viewDistance = 5.0);
|
||||
|
||||
/// This constructor creates an instance from a camera matrix (always of a
|
||||
/// y-Up camera, also see SetPositionAndRotationFromMatrix) and the given
|
||||
/// viewing parameters.
|
||||
GF_API GfFrustum(const GfMatrix4d &camToWorldXf,
|
||||
const GfRange2d &window, const GfRange1d &nearFar,
|
||||
GfFrustum::ProjectionType projectionType,
|
||||
double viewDistance = 5.0);
|
||||
|
||||
/// Copy assignment.
|
||||
GfFrustum &operator=(GfFrustum const &o) noexcept {
|
||||
if (this == &o) {
|
||||
return *this;
|
||||
}
|
||||
_position = o._position;
|
||||
_rotation = o._rotation;
|
||||
_window = o._window;
|
||||
_nearFar = o._nearFar;
|
||||
_viewDistance = o._viewDistance;
|
||||
_projectionType = o._projectionType;
|
||||
delete _planes.load(std::memory_order_relaxed);
|
||||
if (auto *planes = o._planes.load(std::memory_order_relaxed)) {
|
||||
_planes.store(new std::array<GfPlane, 6>(*planes),
|
||||
std::memory_order_relaxed);
|
||||
}
|
||||
else {
|
||||
_planes.store(nullptr, std::memory_order_relaxed);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Move assignment.
|
||||
GfFrustum &operator=(GfFrustum &&o) noexcept {
|
||||
if (this == &o) {
|
||||
return *this;
|
||||
}
|
||||
_position = o._position;
|
||||
_rotation = o._rotation;
|
||||
_window = o._window;
|
||||
_nearFar = o._nearFar;
|
||||
_viewDistance = o._viewDistance;
|
||||
_projectionType = o._projectionType;
|
||||
delete _planes.load(std::memory_order_relaxed);
|
||||
_planes.store(o._planes.load(std::memory_order_relaxed),
|
||||
std::memory_order_relaxed);
|
||||
o._planes.store(nullptr, std::memory_order_relaxed);
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend inline size_t hash_value(const GfFrustum &f) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, f._position);
|
||||
boost::hash_combine(h, f._rotation);
|
||||
boost::hash_combine(h, f._window);
|
||||
boost::hash_combine(h, f._nearFar);
|
||||
boost::hash_combine(h, f._viewDistance);
|
||||
boost::hash_combine(h, f._projectionType);
|
||||
return h;
|
||||
}
|
||||
|
||||
// Equality operator. true iff all parts match.
|
||||
bool operator ==(const GfFrustum& f) const {
|
||||
if (_position != f._position) return false;
|
||||
if (_rotation != f._rotation) return false;
|
||||
if (_window != f._window) return false;
|
||||
if (_nearFar != f._nearFar) return false;
|
||||
if (_viewDistance != f._viewDistance) return false;
|
||||
if (_projectionType != f._projectionType) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Inequality operator. true iff not equality.
|
||||
bool operator !=(const GfFrustum& f) const {
|
||||
return !(*this == f);
|
||||
}
|
||||
|
||||
/// Destructor.
|
||||
GF_API ~GfFrustum();
|
||||
|
||||
/// \name Value setting and access
|
||||
/// The methods in this group set and access the values that are used to
|
||||
/// define a frustum.
|
||||
///@{
|
||||
|
||||
/// Sets the position of the frustum in world space.
|
||||
void SetPosition(const GfVec3d &position) {
|
||||
_position = position;
|
||||
_DirtyFrustumPlanes();
|
||||
}
|
||||
|
||||
/// Returns the position of the frustum in world space.
|
||||
const GfVec3d & GetPosition() const {
|
||||
return _position;
|
||||
}
|
||||
|
||||
/// Sets the orientation of the frustum in world space as a rotation to
|
||||
/// apply to the default frame: looking along the -z axis with the +y axis
|
||||
/// as "up".
|
||||
void SetRotation(const GfRotation &rotation) {
|
||||
_rotation = rotation;
|
||||
_DirtyFrustumPlanes();
|
||||
}
|
||||
|
||||
/// Returns the orientation of the frustum in world space as a rotation to
|
||||
/// apply to the -z axis.
|
||||
const GfRotation & GetRotation() const {
|
||||
return _rotation;
|
||||
}
|
||||
|
||||
/// Sets the position and rotation of the frustum from a camera matrix
|
||||
/// (always from a y-Up camera). The resulting frustum's transform will
|
||||
/// always represent a right-handed and orthonormal coordinate sytem
|
||||
/// (scale, shear, and projection are removed from the given \p
|
||||
/// camToWorldXf).
|
||||
GF_API void SetPositionAndRotationFromMatrix(const GfMatrix4d &camToWorldXf);
|
||||
|
||||
/// Sets the window rectangle in the reference plane that defines the
|
||||
/// left, right, top, and bottom planes of the frustum.
|
||||
void SetWindow(const GfRange2d &window) {
|
||||
_window = window;
|
||||
_DirtyFrustumPlanes();
|
||||
}
|
||||
|
||||
/// Returns the window rectangle in the reference plane.
|
||||
const GfRange2d & GetWindow() const {
|
||||
return _window;
|
||||
}
|
||||
|
||||
/// Returns the depth of the reference plane.
|
||||
static double GetReferencePlaneDepth() {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
/// Sets the near/far interval.
|
||||
void SetNearFar(const GfRange1d &nearFar) {
|
||||
_nearFar = nearFar;
|
||||
_DirtyFrustumPlanes();
|
||||
}
|
||||
|
||||
/// Returns the near/far interval.
|
||||
const GfRange1d & GetNearFar() const {
|
||||
return _nearFar;
|
||||
}
|
||||
|
||||
/// Sets the view distance.
|
||||
void SetViewDistance(double viewDistance) {
|
||||
_viewDistance = viewDistance;
|
||||
}
|
||||
|
||||
/// Returns the view distance.
|
||||
double GetViewDistance() const {
|
||||
return _viewDistance;
|
||||
}
|
||||
|
||||
/// Sets the projection type.
|
||||
void SetProjectionType(GfFrustum::ProjectionType projectionType) {
|
||||
_projectionType = projectionType;
|
||||
_DirtyFrustumPlanes();
|
||||
}
|
||||
|
||||
/// Returns the projection type.
|
||||
GfFrustum::ProjectionType GetProjectionType() const {
|
||||
return _projectionType;
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/// \name Convenience methods
|
||||
///
|
||||
/// The methods in this group allow the frustum's data to be accessed and
|
||||
/// modified in terms of different representations that may be more
|
||||
/// convenient for certain applications.
|
||||
///
|
||||
///@{
|
||||
|
||||
/// Sets up the frustum in a manner similar to \c gluPerspective().
|
||||
///
|
||||
/// It sets the projection type to \c GfFrustum::Perspective and sets the
|
||||
/// window specification so that the resulting symmetric frustum encloses
|
||||
/// an angle of \p fieldOfViewHeight degrees in the vertical direction,
|
||||
/// with \p aspectRatio used to figure the angle in the horizontal
|
||||
/// direction. The near and far distances are specified as well. The
|
||||
/// window coordinates are computed as:
|
||||
/// \code
|
||||
/// top = tan(fieldOfViewHeight / 2)
|
||||
/// bottom = -top
|
||||
/// right = top * aspectRatio
|
||||
/// left = -right
|
||||
/// near = nearDistance
|
||||
/// far = farDistance
|
||||
/// \endcode
|
||||
///
|
||||
GF_API void SetPerspective(double fieldOfViewHeight,
|
||||
double aspectRatio,
|
||||
double nearDistance, double farDistance);
|
||||
|
||||
/// Sets up the frustum in a manner similar to gluPerspective().
|
||||
///
|
||||
/// It sets the projection type to \c GfFrustum::Perspective and
|
||||
/// sets the window specification so that:
|
||||
///
|
||||
/// If \a isFovVertical is true, the resulting symmetric frustum encloses
|
||||
/// an angle of \p fieldOfView degrees in the vertical direction, with \p
|
||||
/// aspectRatio used to figure the angle in the horizontal direction.
|
||||
///
|
||||
/// If \a isFovVertical is false, the resulting symmetric frustum encloses
|
||||
/// an angle of \p fieldOfView degrees in the horizontal direction, with
|
||||
/// \p aspectRatio used to figure the angle in the vertical direction.
|
||||
///
|
||||
/// The near and far distances are specified as well. The window
|
||||
/// coordinates are computed as follows:
|
||||
///
|
||||
/// \li if isFovVertical:
|
||||
/// \li top = tan(fieldOfView / 2)
|
||||
/// \li right = top * aspectRatio
|
||||
/// \li if NOT isFovVertical:
|
||||
/// \li right = tan(fieldOfView / 2)
|
||||
/// \li top = right / aspectRation
|
||||
/// \li bottom = -top
|
||||
/// \li left = -right
|
||||
/// \li near = nearDistance
|
||||
/// \li far = farDistance
|
||||
///
|
||||
GF_API void SetPerspective(double fieldOfView,
|
||||
bool isFovVertical,
|
||||
double aspectRatio,
|
||||
double nearDistance, double farDistance);
|
||||
|
||||
/// Returns the current frustum in the format used by \c SetPerspective().
|
||||
/// If the current frustum is not a perspective projection, this returns
|
||||
/// \c false and leaves the parameters untouched.
|
||||
GF_API bool GetPerspective(double *fieldOfViewHeight,
|
||||
double *aspectRatio,
|
||||
double *nearDistance,
|
||||
double *farDistance) const;
|
||||
|
||||
/// Returns the current frustum in the format used by \c SetPerspective().
|
||||
/// If the current frustum is not a perspective projection, this returns
|
||||
/// \c false and leaves the parameters untouched.
|
||||
GF_API bool GetPerspective(bool isFovVertical,
|
||||
double *fieldOfView,
|
||||
double *aspectRatio,
|
||||
double *nearDistance,
|
||||
double *farDistance) const;
|
||||
|
||||
/// Returns the horizontal or vertical fov of the frustum. The fov of the
|
||||
/// frustum is not necessarily the same value as displayed in the viewer.
|
||||
/// The displayed fov is a function of the focal length or FOV avar. The
|
||||
/// frustum's fov may be different due to things like lens breathing.
|
||||
///
|
||||
/// If the frustum is not of type \c GfFrustum::Perspective, the returned
|
||||
/// FOV will be 0.0.
|
||||
///
|
||||
/// \note The default value for \c isFovVertical is false so calling \c
|
||||
/// GetFOV without an argument will return the horizontal field of view
|
||||
/// which is compatible with menv2x's old GfFrustum::GetFOV routine.
|
||||
GF_API double GetFOV(bool isFovVertical = false);
|
||||
|
||||
/// Sets up the frustum in a manner similar to \c glOrtho().
|
||||
///
|
||||
/// Sets the projection to \c GfFrustum::Orthographic and sets the window
|
||||
/// and near/far specifications based on the given values.
|
||||
GF_API
|
||||
void SetOrthographic(double left, double right,
|
||||
double bottom, double top,
|
||||
double nearPlane, double farPlane);
|
||||
|
||||
/// Returns the current frustum in the format used by \c
|
||||
/// SetOrthographic(). If the current frustum is not an orthographic
|
||||
/// projection, this returns \c false and leaves the parameters untouched.
|
||||
GF_API bool GetOrthographic(double *left, double *right,
|
||||
double *bottom, double *top,
|
||||
double *nearPlane, double *farPlane)
|
||||
const;
|
||||
|
||||
/// Modifies the frustum to tightly enclose a sphere with the given center
|
||||
/// and radius, using the current view direction. The planes of the
|
||||
/// frustum are adjusted as necessary. The given amount of slack is added
|
||||
/// to the sphere's radius is used around the sphere to avoid boundary
|
||||
/// problems.
|
||||
GF_API void FitToSphere(const GfVec3d ¢er,
|
||||
double radius,
|
||||
double slack = 0.0);
|
||||
|
||||
/// Transforms the frustum by the given matrix.
|
||||
///
|
||||
/// The transformation matrix is applied as follows: the position and the
|
||||
/// direction vector are transformed with the given matrix. Then the
|
||||
/// length of the new direction vector is used to rescale the near and far
|
||||
/// plane and the view distance. Finally, the points that define the
|
||||
/// reference plane are transformed by the matrix. This method assures
|
||||
/// that the frustum will not be sheared or perspective-projected.
|
||||
///
|
||||
/// \note Note that this definition means that the transformed frustum
|
||||
/// does not preserve scales very well. Do \em not use this function to
|
||||
/// transform a frustum that is to be used for precise operations such as
|
||||
/// intersection testing.
|
||||
GF_API GfFrustum& Transform(const GfMatrix4d &matrix);
|
||||
|
||||
/// Returns the normalized world-space view direction vector, which is
|
||||
/// computed by rotating the -z axis by the frustum's rotation.
|
||||
GF_API GfVec3d ComputeViewDirection() const;
|
||||
|
||||
/// Returns the normalized world-space up vector, which is computed by
|
||||
/// rotating the y axis by the frustum's rotation.
|
||||
GF_API GfVec3d ComputeUpVector() const;
|
||||
|
||||
/// Computes the view frame defined by this frustum. The frame consists of
|
||||
/// the view direction, up vector and side vector, as shown in this
|
||||
/// diagram.
|
||||
///
|
||||
/// \code
|
||||
/// up
|
||||
/// ^ ^
|
||||
/// | /
|
||||
/// | / view
|
||||
/// |/
|
||||
/// +- - - - > side
|
||||
/// \endcode
|
||||
///
|
||||
GF_API void ComputeViewFrame(GfVec3d *side,
|
||||
GfVec3d *up,
|
||||
GfVec3d *view) const;
|
||||
|
||||
/// Computes and returns the world-space look-at point from the eye point
|
||||
/// (position), view direction (rotation), and view distance.
|
||||
GF_API GfVec3d ComputeLookAtPoint() const;
|
||||
|
||||
/// Returns a matrix that represents the viewing transformation for this
|
||||
/// frustum. That is, it returns the matrix that converts points from
|
||||
/// world space to eye (frustum) space.
|
||||
GF_API GfMatrix4d ComputeViewMatrix() const;
|
||||
|
||||
/// Returns a matrix that represents the inverse viewing transformation
|
||||
/// for this frustum. That is, it returns the matrix that converts points
|
||||
/// from eye (frustum) space to world space.
|
||||
GF_API GfMatrix4d ComputeViewInverse() const;
|
||||
|
||||
/// Returns a GL-style projection matrix corresponding to the frustum's
|
||||
/// projection.
|
||||
GF_API GfMatrix4d ComputeProjectionMatrix() const;
|
||||
|
||||
/// Returns the aspect ratio of the frustum, defined as the width of the
|
||||
/// window divided by the height. If the height is zero or negative, this
|
||||
/// returns 0.
|
||||
GF_API double ComputeAspectRatio() const;
|
||||
|
||||
/// Returns the world-space corners of the frustum as a vector of 8
|
||||
/// points, ordered as:
|
||||
/// \li Left bottom near
|
||||
/// \li Right bottom near
|
||||
/// \li Left top near
|
||||
/// \li Right top near
|
||||
/// \li Left bottom far
|
||||
/// \li Right bottom far
|
||||
/// \li Left top far
|
||||
/// \li Right top far
|
||||
GF_API
|
||||
std::vector<GfVec3d> ComputeCorners() const;
|
||||
|
||||
/// Returns the world-space corners of the intersection of the frustum
|
||||
/// with a plane parallel to the near/far plane at distance d from the
|
||||
/// apex, ordered as:
|
||||
/// \li Left bottom
|
||||
/// \li Right bottom
|
||||
/// \li Left top
|
||||
/// \li Right top
|
||||
/// In particular, it gives the partial result of ComputeCorners when given
|
||||
/// near or far distance.
|
||||
GF_API
|
||||
std::vector<GfVec3d> ComputeCornersAtDistance(double d) const;
|
||||
|
||||
/// Returns a frustum that is a narrowed-down version of this frustum,
|
||||
/// such that the frustum rectangle on the near plane encloses \p point
|
||||
/// with at most \p halfSize[0] distance on the left and right and at most
|
||||
/// \p halfSize[1] distance on the top and bottom. (If \p point is closer
|
||||
/// than the half size to a side of the frustum, that side is left alone.
|
||||
/// The point and sizes are in normalized 2D coordinates; they range from
|
||||
/// (-1, -1) at the lower left corner of the near-plane window rectangle
|
||||
/// to (1,1) at the upper right corner.
|
||||
///
|
||||
/// \p point is a 2d point expressed as a normalized window position.
|
||||
///
|
||||
/// This method is useful for computing a volume to use for interactive
|
||||
/// picking.
|
||||
GF_API GfFrustum ComputeNarrowedFrustum(const GfVec2d &point,
|
||||
const GfVec2d &halfSize) const;
|
||||
|
||||
/// Returns a frustum that is a narrowed-down version of this frustum,
|
||||
/// such that the frustum rectangle on the near plane encloses \p point
|
||||
/// with at most \p halfSize[0] distance on the left and right and at most
|
||||
/// \p halfSize[1] distance on the top and bottom. (If \p point is closer
|
||||
/// than the half size to a side of the frustum, that side is left alone.
|
||||
/// The point and sizes are in normalized 2D coordinates; they range from
|
||||
/// (-1, -1) at the lower left corner of the near-plane window rectangle
|
||||
/// to (1,1) at the upper right corner.
|
||||
///
|
||||
/// \p point is a 3d point expressed in world coordinates
|
||||
///
|
||||
/// This method is useful for computing a volume to use for interactive
|
||||
/// picking.
|
||||
GF_API GfFrustum ComputeNarrowedFrustum(const GfVec3d &worldPoint,
|
||||
const GfVec2d &halfSize) const;
|
||||
|
||||
/// Builds and returns a \c GfRay that starts at the viewpoint and extends
|
||||
/// through the given \a windowPos given in normalized coords (-1 to +1 in
|
||||
/// both dimensions) window position.
|
||||
///
|
||||
/// Contrasted with ComputePickRay(), this method returns a ray whose
|
||||
/// origin is the eyepoint, while that method returns a ray whose origin
|
||||
/// is on the near plane.
|
||||
GF_API GfRay ComputeRay(const GfVec2d &windowPos) const;
|
||||
|
||||
/// Builds and returns a \c GfRay that connects the viewpoint to the given
|
||||
/// 3d point in worldspace.
|
||||
///
|
||||
/// Contrasted with ComputePickRay(), this method returns a ray whose
|
||||
/// origin is the eyepoint, while that method returns a ray whose origin
|
||||
/// is on the near plane.
|
||||
GF_API GfRay ComputeRay(const GfVec3d &worldSpacePos) const;
|
||||
|
||||
/// Builds and returns a \c GfRay that can be used for picking at the
|
||||
/// given normalized (-1 to +1 in both dimensions) window position.
|
||||
///
|
||||
/// Contrasted with ComputeRay(), that method returns a ray whose origin
|
||||
/// is the eyepoint, while this method returns a ray whose origin is on
|
||||
/// the near plane.
|
||||
GF_API GfRay ComputePickRay(const GfVec2d &windowPos) const;
|
||||
|
||||
/// Builds and returns a \c GfRay that can be used for picking that
|
||||
/// connects the viewpoint to the given 3d point in worldspace.
|
||||
GF_API GfRay ComputePickRay(const GfVec3d &worldSpacePos) const;
|
||||
|
||||
///@}
|
||||
|
||||
/// \name Intersection methods
|
||||
///
|
||||
/// The methods in this group implement intersection operations
|
||||
/// between this frustum and a given primitive.
|
||||
///
|
||||
///@{
|
||||
|
||||
/// Returns true if the given axis-aligned bbox is inside or intersecting
|
||||
/// the frustum. Otherwise, it returns false. Useful when doing picking or
|
||||
/// frustum culling.
|
||||
GF_API bool Intersects(const GfBBox3d &bbox) const;
|
||||
|
||||
/// Returns true if the given point is inside or intersecting the frustum.
|
||||
/// Otherwise, it returns false.
|
||||
GF_API bool Intersects(const GfVec3d &point) const;
|
||||
|
||||
/// Returns \c true if the line segment formed by the given points is
|
||||
/// inside or intersecting the frustum. Otherwise, it returns false.
|
||||
GF_API bool Intersects(const GfVec3d &p0,
|
||||
const GfVec3d &p1) const;
|
||||
|
||||
/// Returns \c true if the triangle formed by the given points is inside
|
||||
/// or intersecting the frustum. Otherwise, it returns false.
|
||||
GF_API bool Intersects(const GfVec3d &p0,
|
||||
const GfVec3d &p1,
|
||||
const GfVec3d &p2) const;
|
||||
|
||||
/// Returns \c true if the bbox volume intersects the view volume given by
|
||||
/// the view-projection matrix, erring on the side of false positives for
|
||||
/// efficiency.
|
||||
///
|
||||
/// This method is intended for cases where a GfFrustum is not available
|
||||
/// or when the view-projection matrix yields a view volume that is not
|
||||
/// expressable as a GfFrustum.
|
||||
///
|
||||
/// Because it errs on the side of false positives, it is suitable for
|
||||
/// early-out tests such as draw or intersection culling.
|
||||
///
|
||||
GF_API static bool IntersectsViewVolume(const GfBBox3d &bbox,
|
||||
const GfMatrix4d &vpMat);
|
||||
|
||||
///@}
|
||||
|
||||
private:
|
||||
// Dirty the result of _CalculateFrustumPlanes.
|
||||
GF_API void _DirtyFrustumPlanes();
|
||||
|
||||
// Calculates cached frustum planes used for intersection tests.
|
||||
GF_API void _CalculateFrustumPlanes() const;
|
||||
|
||||
// Builds and returns a \c GfRay that can be used for picking. Given an
|
||||
// eye position and direction in camera space, offsets the ray to emanate
|
||||
// from the near plane, then transforms into worldspace
|
||||
GF_API GfRay _ComputePickRayOffsetToNearPlane(
|
||||
const GfVec3d &camSpaceFrom,
|
||||
const GfVec3d &camSpaceDir) const;
|
||||
|
||||
// Returns a frustum that is a narrowed-down version of this frustum, such
|
||||
// that the frustum rectangle on the near plane encloses \p point with at
|
||||
// most \p halfSize[0] distance on the left and right and at most \p
|
||||
// halfSize[1] distance on the top and bottom. (If \p point is closer than
|
||||
// the half size to a side of the frustum, that side is left alone. The
|
||||
// point and sizes are in normalized 2D coordinates; they range from (-1,
|
||||
// -1) at the lower left corner of the near-plane window rectangle to
|
||||
// (1,1) at the upper right corner.
|
||||
//
|
||||
// \p windowPoint is expressed in window coordinates
|
||||
//
|
||||
// This method is useful for computing a volume to use for interactive
|
||||
// picking.
|
||||
GfFrustum _ComputeNarrowedFrustumSub(const GfVec2d windowPoint,
|
||||
const GfVec2d &halfSize) const;
|
||||
|
||||
bool _SegmentIntersects(GfVec3d const &p0, uint32_t p0Mask,
|
||||
GfVec3d const &p1, uint32_t p1Mask) const;
|
||||
|
||||
// Position of the frustum in world space.
|
||||
GfVec3d _position;
|
||||
|
||||
// Orientation of the frustum in world space as a rotation to apply to the
|
||||
// -z axis.
|
||||
GfRotation _rotation;
|
||||
|
||||
// Window rectangle in the image plane.
|
||||
GfRange2d _window;
|
||||
|
||||
// Near/far interval.
|
||||
GfRange1d _nearFar;
|
||||
|
||||
// View distance.
|
||||
double _viewDistance;
|
||||
|
||||
// Projection type.
|
||||
ProjectionType _projectionType;
|
||||
|
||||
// Cached planes.
|
||||
// If null, the planes have not been calculated.
|
||||
mutable std::atomic<std::array<GfPlane, 6> *> _planes;
|
||||
};
|
||||
|
||||
/// Output a GfFrustum using the format [(position) (rotation) [window]
|
||||
/// [nearFar] viewDistance type]
|
||||
///
|
||||
/// The "type" is "perspective", or "orthographic, depending on the
|
||||
/// projection type of the frustum.
|
||||
///
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream& out, const GfFrustum& f);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_FRUSTUM_H
|
||||
@@ -0,0 +1,106 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_GAMMA_H
|
||||
#define PXR_BASE_GF_GAMMA_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
/// \file gf/gamma.h
|
||||
/// Utilities to map colors between gamma spaces.
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec3f;
|
||||
class GfVec3d;
|
||||
class GfVec4f;
|
||||
class GfVec4d;
|
||||
class GfVec3h;
|
||||
class GfVec4h;
|
||||
|
||||
/// Return a new vector with each component of \p v raised to the power \p
|
||||
/// gamma
|
||||
GF_API
|
||||
GfVec3f GfApplyGamma(const GfVec3f &v, double gamma);
|
||||
|
||||
/// Return a new vector with each component of \p v raised to the power \p
|
||||
/// gamma
|
||||
GF_API
|
||||
GfVec3d GfApplyGamma(const GfVec3d &v, double gamma);
|
||||
|
||||
/// \copydoc GfApplyGamma(GfVec3d,double)
|
||||
GF_API
|
||||
GfVec3h GfApplyGamma(const GfVec3h &v, double gamma);
|
||||
|
||||
/// Return a new vector with the first three components of \p v raised to the
|
||||
/// power \p gamma and the fourth component unchanged.
|
||||
GF_API
|
||||
GfVec4f GfApplyGamma(const GfVec4f &v, double gamma);
|
||||
|
||||
/// Return a new vector with the first three components of \p v raised to the
|
||||
/// power \p gamma and the fourth component unchanged.
|
||||
GF_API
|
||||
GfVec4d GfApplyGamma(const GfVec4d &v, double gamma);
|
||||
|
||||
/// \copydoc GfApplyGamma(GfVec4h,double)
|
||||
GF_API
|
||||
GfVec4h GfApplyGamma(const GfVec4h &v, double gamma);
|
||||
|
||||
/// Return a new float raised to the power \p gamma
|
||||
GF_API
|
||||
float GfApplyGamma(const float &v, double gamma);
|
||||
|
||||
/// Return a new char raised to the power \p gamma
|
||||
GF_API
|
||||
unsigned char GfApplyGamma(const unsigned char &v, double gamma);
|
||||
|
||||
/// Return the system display gamma
|
||||
GF_API
|
||||
double GfGetDisplayGamma();
|
||||
|
||||
/// Given a vec, \p v, representing an energy-linear RGB(A) color, return a
|
||||
/// vec of the same type converted to the system's display gamma.
|
||||
GF_API GfVec3f GfConvertLinearToDisplay(const GfVec3f &v);
|
||||
GF_API GfVec3d GfConvertLinearToDisplay(const GfVec3d &v);
|
||||
GF_API GfVec3h GfConvertLinearToDisplay(const GfVec3h &v);
|
||||
GF_API GfVec4f GfConvertLinearToDisplay(const GfVec4f &v);
|
||||
GF_API GfVec4d GfConvertLinearToDisplay(const GfVec4d &v);
|
||||
GF_API GfVec4h GfConvertLinearToDisplay(const GfVec4h &v);
|
||||
GF_API float GfConvertLinearToDisplay(const float &v);
|
||||
GF_API unsigned char GfConvertLinearToDisplay(const unsigned char &v);
|
||||
|
||||
/// Given a vec, \p v, representing an RGB(A) color in the system's display
|
||||
/// gamma space, return an energy-linear vec of the same type.
|
||||
GF_API GfVec3f GfConvertDisplayToLinear(const GfVec3f &v);
|
||||
GF_API GfVec3d GfConvertDisplayToLinear(const GfVec3d &v);
|
||||
GF_API GfVec3h GfConvertDisplayToLinear(const GfVec3h &v);
|
||||
GF_API GfVec4f GfConvertDisplayToLinear(const GfVec4f &v);
|
||||
GF_API GfVec4d GfConvertDisplayToLinear(const GfVec4d &v);
|
||||
GF_API GfVec4h GfConvertDisplayToLinear(const GfVec4h &v);
|
||||
GF_API float GfConvertDisplayToLinear(const float &v);
|
||||
GF_API unsigned char GfConvertDisplayToLinear(const unsigned char &v);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_GAMMA_H
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_HALF_H
|
||||
#define PXR_BASE_GF_HALF_H
|
||||
|
||||
/// \file gf/half.h
|
||||
///
|
||||
/// This header serves to simply bring in the half float datatype and
|
||||
/// provide a hash_value function. For documentation, of the half type,
|
||||
/// please see the half header in ilmbase_half.h.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/ilmbase_half.h"
|
||||
#include "pxr/base/gf/ilmbase_halfLimits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// A 16-bit floating point data type.
|
||||
using GfHalf = pxr_half::half;
|
||||
|
||||
namespace pxr_half {
|
||||
/// Overload hash_value for half.
|
||||
template<typename Half>
|
||||
inline
|
||||
typename std::enable_if<std::is_same<Half, half>::value, size_t>::type
|
||||
hash_value(const Half& h) { return h.bits(); }
|
||||
}
|
||||
|
||||
template <>
|
||||
struct GfIsFloatingPoint<GfHalf> :
|
||||
public std::integral_constant<bool, true>{};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
|
||||
#endif // PXR_BASE_GF_HALF_H
|
||||
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_HOMOGENEOUS_H
|
||||
#define PXR_BASE_GF_HOMOGENEOUS_H
|
||||
|
||||
/// \file gf/homogeneous.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
/// Utility functions for GfVec4f and GfVec4d as homogeneous vectors
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/vec4d.h"
|
||||
#include "pxr/base/gf/vec4f.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Returns a vector which is \p v homogenized. If the fourth element of \p v
|
||||
/// is 0, it is set to 1.
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
GF_API
|
||||
GfVec4f GfGetHomogenized(const GfVec4f &v);
|
||||
|
||||
/// Homogenizes \p a and \p b and then performs the cross product on the first
|
||||
/// three elements of each. Returns the cross product as a homogenized
|
||||
/// vector.
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
GF_API
|
||||
GfVec4f GfHomogeneousCross(const GfVec4f &a, const GfVec4f &b);
|
||||
|
||||
GF_API
|
||||
GfVec4d GfGetHomogenized(const GfVec4d &v);
|
||||
|
||||
/// Homogenizes \p a and \p b and then performs the cross product on the first
|
||||
/// three elements of each. Returns the cross product as a homogenized
|
||||
/// vector.
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
GF_API
|
||||
GfVec4d GfHomogeneousCross(const GfVec4d &a, const GfVec4d &b);
|
||||
|
||||
/// Projects homogeneous \p v into Euclidean space and returns the result as a
|
||||
/// Vec3f.
|
||||
inline GfVec3f GfProject(const GfVec4f &v) {
|
||||
float inv = (v[3] != 0.0f) ? 1.0f/v[3] : 1.0f;
|
||||
return GfVec3f(inv * v[0], inv * v[1], inv * v[2]);
|
||||
}
|
||||
|
||||
/// Projects homogeneous \p v into Euclidean space and returns the result as a
|
||||
/// Vec3d.
|
||||
inline GfVec3d GfProject(const GfVec4d &v) {
|
||||
double inv = (v[3] != 0.0) ? 1.0/v[3] : 1.0;
|
||||
return GfVec3d(inv * v[0], inv * v[1], inv * v[2]);
|
||||
}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif /* PXR_BASE_GF_HOMOGENEOUS_H */
|
||||
@@ -0,0 +1,759 @@
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
|
||||
// Digital Ltd. LLC
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Industrial Light & Magic nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Primary authors:
|
||||
// Florian Kainz <kainz@ilm.com>
|
||||
// Rod Bogart <rgb@ilm.com>
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// half -- a 16-bit floating point number class:
|
||||
//
|
||||
// Type half can represent positive and negative numbers whose
|
||||
// magnitude is between roughly 6.1e-5 and 6.5e+4 with a relative
|
||||
// error of 9.8e-4; numbers smaller than 6.1e-5 can be represented
|
||||
// with an absolute error of 6.0e-8. All integers from -2048 to
|
||||
// +2048 can be represented exactly.
|
||||
//
|
||||
// Type half behaves (almost) like the built-in C++ floating point
|
||||
// types. In arithmetic expressions, half, float and double can be
|
||||
// mixed freely. Here are a few examples:
|
||||
//
|
||||
// half a (3.5);
|
||||
// float b (a + sqrt (a));
|
||||
// a += b;
|
||||
// b += a;
|
||||
// b = a + 7;
|
||||
//
|
||||
// Conversions from half to float are lossless; all half numbers
|
||||
// are exactly representable as floats.
|
||||
//
|
||||
// Conversions from float to half may not preserve a float's value
|
||||
// exactly. If a float is not representable as a half, then the
|
||||
// float value is rounded to the nearest representable half. If a
|
||||
// float value is exactly in the middle between the two closest
|
||||
// representable half values, then the float value is rounded to
|
||||
// the closest half whose least significant bit is zero.
|
||||
//
|
||||
// Overflows during float-to-half conversions cause arithmetic
|
||||
// exceptions. An overflow occurs when the float value to be
|
||||
// converted is too large to be represented as a half, or if the
|
||||
// float value is an infinity or a NAN.
|
||||
//
|
||||
// The implementation of type half makes the following assumptions
|
||||
// about the implementation of the built-in C++ types:
|
||||
//
|
||||
// float is an IEEE 754 single-precision number
|
||||
// sizeof (float) == 4
|
||||
// sizeof (unsigned int) == sizeof (float)
|
||||
// alignof (unsigned int) == alignof (float)
|
||||
// sizeof (unsigned short) == 2
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#ifndef PXR_HALF_H
|
||||
#define PXR_HALF_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
namespace pxr_half {
|
||||
|
||||
class half
|
||||
{
|
||||
public:
|
||||
|
||||
//-------------
|
||||
// Constructors
|
||||
//-------------
|
||||
|
||||
half () = default; // no initialization
|
||||
half (float f);
|
||||
// rule of 5
|
||||
~half () = default;
|
||||
constexpr half (const half &) noexcept = default;
|
||||
constexpr half (half &&) noexcept = default;
|
||||
|
||||
//--------------------
|
||||
// Conversion to float
|
||||
//--------------------
|
||||
|
||||
operator float () const;
|
||||
|
||||
|
||||
//------------
|
||||
// Unary minus
|
||||
//------------
|
||||
|
||||
half operator - () const;
|
||||
|
||||
|
||||
//-----------
|
||||
// Assignment
|
||||
//-----------
|
||||
|
||||
half & operator = (const half &h) = default;
|
||||
half & operator = (half &&h) noexcept = default;
|
||||
half & operator = (float f);
|
||||
|
||||
half & operator += (half h);
|
||||
half & operator += (float f);
|
||||
|
||||
half & operator -= (half h);
|
||||
half & operator -= (float f);
|
||||
|
||||
half & operator *= (half h);
|
||||
half & operator *= (float f);
|
||||
|
||||
half & operator /= (half h);
|
||||
half & operator /= (float f);
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
// Round to n-bit precision (n should be between 0 and 10).
|
||||
// After rounding, the significand's 10-n least significant
|
||||
// bits will be zero.
|
||||
//---------------------------------------------------------
|
||||
|
||||
half round (unsigned int n) const;
|
||||
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// Classification:
|
||||
//
|
||||
// h.isFinite() returns true if h is a normalized number,
|
||||
// a denormalized number or zero
|
||||
//
|
||||
// h.isNormalized() returns true if h is a normalized number
|
||||
//
|
||||
// h.isDenormalized() returns true if h is a denormalized number
|
||||
//
|
||||
// h.isZero() returns true if h is zero
|
||||
//
|
||||
// h.isNan() returns true if h is a NAN
|
||||
//
|
||||
// h.isInfinity() returns true if h is a positive
|
||||
// or a negative infinity
|
||||
//
|
||||
// h.isNegative() returns true if the sign bit of h
|
||||
// is set (negative)
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
bool isFinite () const;
|
||||
bool isNormalized () const;
|
||||
bool isDenormalized () const;
|
||||
bool isZero () const;
|
||||
bool isNan () const;
|
||||
bool isInfinity () const;
|
||||
bool isNegative () const;
|
||||
|
||||
|
||||
//--------------------------------------------
|
||||
// Special values
|
||||
//
|
||||
// posInf() returns +infinity
|
||||
//
|
||||
// negInf() returns -infinity
|
||||
//
|
||||
// qNan() returns a NAN with the bit
|
||||
// pattern 0111111111111111
|
||||
//
|
||||
// sNan() returns a NAN with the bit
|
||||
// pattern 0111110111111111
|
||||
//--------------------------------------------
|
||||
|
||||
static half posInf ();
|
||||
static half negInf ();
|
||||
static half qNan ();
|
||||
static half sNan ();
|
||||
|
||||
|
||||
//--------------------------------------
|
||||
// Access to the internal representation
|
||||
//--------------------------------------
|
||||
|
||||
GF_API unsigned short bits () const;
|
||||
GF_API void setBits (unsigned short bits);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
union uif
|
||||
{
|
||||
unsigned int i;
|
||||
float f;
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
GF_API static short convert (int i);
|
||||
GF_API static float overflow ();
|
||||
|
||||
unsigned short _h;
|
||||
|
||||
GF_API static const uif _toFloat[1 << 16];
|
||||
GF_API static const unsigned short _eLut[1 << 9];
|
||||
};
|
||||
|
||||
|
||||
|
||||
//-----------
|
||||
// Stream I/O
|
||||
//-----------
|
||||
|
||||
GF_API std::ostream & operator << (std::ostream &os, half h);
|
||||
GF_API std::istream & operator >> (std::istream &is, half &h);
|
||||
|
||||
|
||||
//----------
|
||||
// Debugging
|
||||
//----------
|
||||
|
||||
GF_API void printBits (std::ostream &os, half h);
|
||||
GF_API void printBits (std::ostream &os, float f);
|
||||
GF_API void printBits (char c[19], half h);
|
||||
GF_API void printBits (char c[35], float f);
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Limits
|
||||
//
|
||||
// Visual C++ will complain if PXR_HALF_MIN, PXR_HALF_NRM_MIN etc. are not float
|
||||
// constants, but at least one other compiler (gcc 2.96) produces incorrect
|
||||
// results if they are.
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
|
||||
|
||||
#define PXR_HALF_MIN 5.96046448e-08f // Smallest positive half
|
||||
|
||||
#define PXR_HALF_NRM_MIN 6.10351562e-05f // Smallest positive normalized half
|
||||
|
||||
#define PXR_HALF_MAX 65504.0f // Largest positive half
|
||||
|
||||
#define PXR_HALF_EPSILON 0.00097656f // Smallest positive e for which
|
||||
// half (1.0 + e) != half (1.0)
|
||||
#else
|
||||
|
||||
#define PXR_HALF_MIN 5.96046448e-08 // Smallest positive half
|
||||
|
||||
#define PXR_HALF_NRM_MIN 6.10351562e-05 // Smallest positive normalized half
|
||||
|
||||
#define PXR_HALF_MAX 65504.0 // Largest positive half
|
||||
|
||||
#define PXR_HALF_EPSILON 0.00097656 // Smallest positive e for which
|
||||
// half (1.0 + e) != half (1.0)
|
||||
#endif
|
||||
|
||||
|
||||
#define PXR_HALF_MANT_DIG 11 // Number of digits in mantissa
|
||||
// (significand + hidden leading 1)
|
||||
|
||||
//
|
||||
// floor( (PXR_HALF_MANT_DIG - 1) * log10(2) ) => 3.01... -> 3
|
||||
#define PXR_HALF_DIG 3 // Number of base 10 digits that
|
||||
// can be represented without change
|
||||
|
||||
// ceil(PXR_HALF_MANT_DIG * log10(2) + 1) => 4.31... -> 5
|
||||
#define PXR_HALF_DECIMAL_DIG 5 // Number of base-10 digits that are
|
||||
// necessary to uniquely represent all
|
||||
// distinct values
|
||||
|
||||
#define PXR_HALF_RADIX 2 // Base of the exponent
|
||||
|
||||
#define PXR_HALF_MIN_EXP -13 // Minimum negative integer such that
|
||||
// PXR_HALF_RADIX raised to the power of
|
||||
// one less than that integer is a
|
||||
// normalized half
|
||||
|
||||
#define PXR_HALF_MAX_EXP 16 // Maximum positive integer such that
|
||||
// PXR_HALF_RADIX raised to the power of
|
||||
// one less than that integer is a
|
||||
// normalized half
|
||||
|
||||
#define PXR_HALF_MIN_10_EXP -4 // Minimum positive integer such
|
||||
// that 10 raised to that power is
|
||||
// a normalized half
|
||||
|
||||
#define PXR_HALF_MAX_10_EXP 4 // Maximum positive integer such
|
||||
// that 10 raised to that power is
|
||||
// a normalized half
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// Implementation --
|
||||
//
|
||||
// Representation of a float:
|
||||
//
|
||||
// We assume that a float, f, is an IEEE 754 single-precision
|
||||
// floating point number, whose bits are arranged as follows:
|
||||
//
|
||||
// 31 (msb)
|
||||
// |
|
||||
// | 30 23
|
||||
// | | |
|
||||
// | | | 22 0 (lsb)
|
||||
// | | | | |
|
||||
// X XXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX
|
||||
//
|
||||
// s e m
|
||||
//
|
||||
// S is the sign-bit, e is the exponent and m is the significand.
|
||||
//
|
||||
// If e is between 1 and 254, f is a normalized number:
|
||||
//
|
||||
// s e-127
|
||||
// f = (-1) * 2 * 1.m
|
||||
//
|
||||
// If e is 0, and m is not zero, f is a denormalized number:
|
||||
//
|
||||
// s -126
|
||||
// f = (-1) * 2 * 0.m
|
||||
//
|
||||
// If e and m are both zero, f is zero:
|
||||
//
|
||||
// f = 0.0
|
||||
//
|
||||
// If e is 255, f is an "infinity" or "not a number" (NAN),
|
||||
// depending on whether m is zero or not.
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// 0 00000000 00000000000000000000000 = 0.0
|
||||
// 0 01111110 00000000000000000000000 = 0.5
|
||||
// 0 01111111 00000000000000000000000 = 1.0
|
||||
// 0 10000000 00000000000000000000000 = 2.0
|
||||
// 0 10000000 10000000000000000000000 = 3.0
|
||||
// 1 10000101 11110000010000000000000 = -124.0625
|
||||
// 0 11111111 00000000000000000000000 = +infinity
|
||||
// 1 11111111 00000000000000000000000 = -infinity
|
||||
// 0 11111111 10000000000000000000000 = NAN
|
||||
// 1 11111111 11111111111111111111111 = NAN
|
||||
//
|
||||
// Representation of a half:
|
||||
//
|
||||
// Here is the bit-layout for a half number, h:
|
||||
//
|
||||
// 15 (msb)
|
||||
// |
|
||||
// | 14 10
|
||||
// | | |
|
||||
// | | | 9 0 (lsb)
|
||||
// | | | | |
|
||||
// X XXXXX XXXXXXXXXX
|
||||
//
|
||||
// s e m
|
||||
//
|
||||
// S is the sign-bit, e is the exponent and m is the significand.
|
||||
//
|
||||
// If e is between 1 and 30, h is a normalized number:
|
||||
//
|
||||
// s e-15
|
||||
// h = (-1) * 2 * 1.m
|
||||
//
|
||||
// If e is 0, and m is not zero, h is a denormalized number:
|
||||
//
|
||||
// S -14
|
||||
// h = (-1) * 2 * 0.m
|
||||
//
|
||||
// If e and m are both zero, h is zero:
|
||||
//
|
||||
// h = 0.0
|
||||
//
|
||||
// If e is 31, h is an "infinity" or "not a number" (NAN),
|
||||
// depending on whether m is zero or not.
|
||||
//
|
||||
// Examples:
|
||||
//
|
||||
// 0 00000 0000000000 = 0.0
|
||||
// 0 01110 0000000000 = 0.5
|
||||
// 0 01111 0000000000 = 1.0
|
||||
// 0 10000 0000000000 = 2.0
|
||||
// 0 10000 1000000000 = 3.0
|
||||
// 1 10101 1111000001 = -124.0625
|
||||
// 0 11111 0000000000 = +infinity
|
||||
// 1 11111 0000000000 = -infinity
|
||||
// 0 11111 1000000000 = NAN
|
||||
// 1 11111 1111111111 = NAN
|
||||
//
|
||||
// Conversion:
|
||||
//
|
||||
// Converting from a float to a half requires some non-trivial bit
|
||||
// manipulations. In some cases, this makes conversion relatively
|
||||
// slow, but the most common case is accelerated via table lookups.
|
||||
//
|
||||
// Converting back from a half to a float is easier because we don't
|
||||
// have to do any rounding. In addition, there are only 65536
|
||||
// different half numbers; we can convert each of those numbers once
|
||||
// and store the results in a table. Later, all conversions can be
|
||||
// done using only simple table lookups.
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
//----------------------------
|
||||
// Half-from-float constructor
|
||||
//----------------------------
|
||||
|
||||
inline
|
||||
half::half (float f)
|
||||
{
|
||||
uif x;
|
||||
|
||||
x.f = f;
|
||||
|
||||
if (f == 0)
|
||||
{
|
||||
//
|
||||
// Common special case - zero.
|
||||
// Preserve the zero's sign bit.
|
||||
//
|
||||
|
||||
_h = (x.i >> 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// We extract the combined sign and exponent, e, from our
|
||||
// floating-point number, f. Then we convert e to the sign
|
||||
// and exponent of the half number via a table lookup.
|
||||
//
|
||||
// For the most common case, where a normalized half is produced,
|
||||
// the table lookup returns a non-zero value; in this case, all
|
||||
// we have to do is round f's significand to 10 bits and combine
|
||||
// the result with e.
|
||||
//
|
||||
// For all other cases (overflow, zeroes, denormalized numbers
|
||||
// resulting from underflow, infinities and NANs), the table
|
||||
// lookup returns zero, and we call a longer, non-inline function
|
||||
// to do the float-to-half conversion.
|
||||
//
|
||||
|
||||
int e = (x.i >> 23) & 0x000001ff;
|
||||
|
||||
e = _eLut[e];
|
||||
|
||||
if (e)
|
||||
{
|
||||
//
|
||||
// Simple case - round the significand, m, to 10
|
||||
// bits and combine it with the sign and exponent.
|
||||
//
|
||||
|
||||
int m = x.i & 0x007fffff;
|
||||
_h = e + ((m + 0x00000fff + ((m >> 13) & 1)) >> 13);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Difficult case - call a function.
|
||||
//
|
||||
|
||||
_h = convert (x.i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------
|
||||
// Half-to-float conversion via table lookup
|
||||
//------------------------------------------
|
||||
|
||||
inline
|
||||
half::operator float () const
|
||||
{
|
||||
return _toFloat[_h].f;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------
|
||||
// Round to n-bit precision
|
||||
//-------------------------
|
||||
|
||||
inline half
|
||||
half::round (unsigned int n) const
|
||||
{
|
||||
//
|
||||
// Parameter check.
|
||||
//
|
||||
|
||||
if (n >= 10)
|
||||
return *this;
|
||||
|
||||
//
|
||||
// Disassemble h into the sign, s,
|
||||
// and the combined exponent and significand, e.
|
||||
//
|
||||
|
||||
unsigned short s = _h & 0x8000;
|
||||
unsigned short e = _h & 0x7fff;
|
||||
|
||||
//
|
||||
// Round the exponent and significand to the nearest value
|
||||
// where ones occur only in the (10-n) most significant bits.
|
||||
// Note that the exponent adjusts automatically if rounding
|
||||
// up causes the significand to overflow.
|
||||
//
|
||||
|
||||
e >>= 9 - n;
|
||||
e += e & 1;
|
||||
e <<= 9 - n;
|
||||
|
||||
//
|
||||
// Check for exponent overflow.
|
||||
//
|
||||
|
||||
if (e >= 0x7c00)
|
||||
{
|
||||
//
|
||||
// Overflow occurred -- truncate instead of rounding.
|
||||
//
|
||||
|
||||
e = _h;
|
||||
e >>= 10 - n;
|
||||
e <<= 10 - n;
|
||||
}
|
||||
|
||||
//
|
||||
// Put the original sign bit back.
|
||||
//
|
||||
|
||||
half h;
|
||||
h._h = s | e;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------
|
||||
// Other inline functions
|
||||
//-----------------------
|
||||
|
||||
inline half
|
||||
half::operator - () const
|
||||
{
|
||||
half h;
|
||||
h._h = _h ^ 0x8000;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator = (float f)
|
||||
{
|
||||
*this = half (f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator += (half h)
|
||||
{
|
||||
*this = half (float (*this) + float (h));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator += (float f)
|
||||
{
|
||||
*this = half (float (*this) + f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator -= (half h)
|
||||
{
|
||||
*this = half (float (*this) - float (h));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator -= (float f)
|
||||
{
|
||||
*this = half (float (*this) - f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator *= (half h)
|
||||
{
|
||||
*this = half (float (*this) * float (h));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator *= (float f)
|
||||
{
|
||||
*this = half (float (*this) * f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator /= (half h)
|
||||
{
|
||||
*this = half (float (*this) / float (h));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline half &
|
||||
half::operator /= (float f)
|
||||
{
|
||||
*this = half (float (*this) / f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isFinite () const
|
||||
{
|
||||
unsigned short e = (_h >> 10) & 0x001f;
|
||||
return e < 31;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isNormalized () const
|
||||
{
|
||||
unsigned short e = (_h >> 10) & 0x001f;
|
||||
return e > 0 && e < 31;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isDenormalized () const
|
||||
{
|
||||
unsigned short e = (_h >> 10) & 0x001f;
|
||||
unsigned short m = _h & 0x3ff;
|
||||
return e == 0 && m != 0;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isZero () const
|
||||
{
|
||||
return (_h & 0x7fff) == 0;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isNan () const
|
||||
{
|
||||
unsigned short e = (_h >> 10) & 0x001f;
|
||||
unsigned short m = _h & 0x3ff;
|
||||
return e == 31 && m != 0;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isInfinity () const
|
||||
{
|
||||
unsigned short e = (_h >> 10) & 0x001f;
|
||||
unsigned short m = _h & 0x3ff;
|
||||
return e == 31 && m == 0;
|
||||
}
|
||||
|
||||
|
||||
inline bool
|
||||
half::isNegative () const
|
||||
{
|
||||
return (_h & 0x8000) != 0;
|
||||
}
|
||||
|
||||
|
||||
inline half
|
||||
half::posInf ()
|
||||
{
|
||||
half h;
|
||||
h._h = 0x7c00;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
inline half
|
||||
half::negInf ()
|
||||
{
|
||||
half h;
|
||||
h._h = 0xfc00;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
inline half
|
||||
half::qNan ()
|
||||
{
|
||||
half h;
|
||||
h._h = 0x7fff;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
inline half
|
||||
half::sNan ()
|
||||
{
|
||||
half h;
|
||||
h._h = 0x7dff;
|
||||
return h;
|
||||
}
|
||||
|
||||
|
||||
inline unsigned short
|
||||
half::bits () const
|
||||
{
|
||||
return _h;
|
||||
}
|
||||
|
||||
|
||||
inline void
|
||||
half::setBits (unsigned short bits)
|
||||
{
|
||||
_h = bits;
|
||||
}
|
||||
|
||||
} // namespace pxr_half
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,114 @@
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
|
||||
// Digital Ltd. LLC
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Industrial Light & Magic nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// Primary authors:
|
||||
// Florian Kainz <kainz@ilm.com>
|
||||
// Rod Bogart <rgb@ilm.com>
|
||||
|
||||
|
||||
#ifndef PXR_HALF_LIMITS_H
|
||||
#define PXR_HALF_LIMITS_H
|
||||
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
//
|
||||
// C++ standard library-style numeric_limits for class half
|
||||
//
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
#include <limits>
|
||||
#include "pxr/base/gf/ilmbase_half.h"
|
||||
|
||||
namespace std {
|
||||
|
||||
template <>
|
||||
class numeric_limits <PXR_NS::pxr_half::half>
|
||||
{
|
||||
private:
|
||||
using half = PXR_NS::pxr_half::half;
|
||||
|
||||
public:
|
||||
|
||||
static const bool is_specialized = true;
|
||||
|
||||
static half min () throw () {return PXR_HALF_NRM_MIN;}
|
||||
static half max () throw () {return PXR_HALF_MAX;}
|
||||
|
||||
static const int digits = PXR_HALF_MANT_DIG;
|
||||
static const int digits10 = PXR_HALF_DIG;
|
||||
static const bool is_signed = true;
|
||||
static const bool is_integer = false;
|
||||
static const bool is_exact = false;
|
||||
static const int radix = PXR_HALF_RADIX;
|
||||
static half epsilon () throw () {return PXR_HALF_EPSILON;}
|
||||
static half round_error () throw () {return PXR_HALF_EPSILON / 2;}
|
||||
|
||||
static const int min_exponent = PXR_HALF_MIN_EXP;
|
||||
static const int min_exponent10 = PXR_HALF_MIN_10_EXP;
|
||||
static const int max_exponent = PXR_HALF_MAX_EXP;
|
||||
static const int max_exponent10 = PXR_HALF_MAX_10_EXP;
|
||||
|
||||
static const bool has_infinity = true;
|
||||
static const bool has_quiet_NaN = true;
|
||||
static const bool has_signaling_NaN = true;
|
||||
static const float_denorm_style has_denorm = denorm_present;
|
||||
static const bool has_denorm_loss = false;
|
||||
static half infinity () throw () {return half::posInf();}
|
||||
static half quiet_NaN () throw () {return half::qNan();}
|
||||
static half signaling_NaN () throw () {return half::sNan();}
|
||||
static half denorm_min () throw () {return PXR_HALF_MIN;}
|
||||
|
||||
static const bool is_iec559 = false;
|
||||
static const bool is_bounded = false;
|
||||
static const bool is_modulo = false;
|
||||
|
||||
static const bool traps = true;
|
||||
static const bool tinyness_before = false;
|
||||
static const float_round_style round_style = round_to_nearest;
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
|
||||
// C++11 additions.
|
||||
static constexpr int max_digits10 = PXR_HALF_DECIMAL_DIG;
|
||||
static half lowest () {return -PXR_HALF_MAX;}
|
||||
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,427 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_INTERVAL_H
|
||||
#define PXR_BASE_GF_INTERVAL_H
|
||||
|
||||
/// \file gf/interval.h
|
||||
/// \ingroup group_gf_BasicMath
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <float.h>
|
||||
#include <iosfwd>
|
||||
#include <limits>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfInterval
|
||||
/// \ingroup group_gf_BasicMath
|
||||
///
|
||||
/// A basic mathematical interval class.
|
||||
///
|
||||
/// Can represent intervals with either open or closed boundary
|
||||
/// conditions.
|
||||
///
|
||||
class GfInterval
|
||||
{
|
||||
public:
|
||||
/// \name Constructors
|
||||
///@{
|
||||
|
||||
/// Construct an empty open interval, (0,0).
|
||||
GfInterval() :
|
||||
_min(0.0, false),
|
||||
_max(0.0, false)
|
||||
{}
|
||||
|
||||
/// Construct a closed interval representing the single point, as [val,val].
|
||||
GfInterval(double val) :
|
||||
_min(val, true),
|
||||
_max(val, true)
|
||||
{}
|
||||
|
||||
/// Construct an interval with the given arguments.
|
||||
GfInterval(double min, double max,
|
||||
bool minClosed=true, bool maxClosed=true) :
|
||||
_min(min, minClosed),
|
||||
_max(max, maxClosed)
|
||||
{}
|
||||
|
||||
///@}
|
||||
|
||||
/// Equality operator.
|
||||
bool operator==(const GfInterval &rhs) const {
|
||||
return _min == rhs._min && _max == rhs._max;
|
||||
}
|
||||
|
||||
/// Inequality operator.
|
||||
bool operator!=(const GfInterval &rhs) const {
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
/// Less-than operator.
|
||||
bool operator<(const GfInterval &rhs) const {
|
||||
// Compare min bound
|
||||
if (_min != rhs._min)
|
||||
return _min < rhs._min;
|
||||
|
||||
// Compare max bound
|
||||
if (_max != rhs._max)
|
||||
return _max < rhs._max;
|
||||
|
||||
// Equal
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Hash value.
|
||||
/// Just a basic hash function, not particularly high quality.
|
||||
size_t Hash() const { return hash_value(*this); }
|
||||
|
||||
friend inline size_t hash_value(GfInterval const &i) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, i._min);
|
||||
boost::hash_combine(h, i._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Minimum value
|
||||
double GetMin() const { return _min.value; }
|
||||
|
||||
/// Maximum value
|
||||
double GetMax() const { return _max.value; }
|
||||
|
||||
/// Set minimum value
|
||||
void SetMin(double v) {
|
||||
_min = _Bound(v, _min.closed);
|
||||
}
|
||||
|
||||
/// Set minimum value and boundary condition
|
||||
void SetMin(double v, bool minClosed ) {
|
||||
_min = _Bound(v, minClosed);
|
||||
}
|
||||
|
||||
/// Set maximum value
|
||||
void SetMax(double v) {
|
||||
_max = _Bound(v, _max.closed);
|
||||
}
|
||||
|
||||
/// Set maximum value and boundary condition
|
||||
void SetMax(double v, bool maxClosed ) {
|
||||
_max = _Bound(v, maxClosed);
|
||||
}
|
||||
|
||||
/// Minimum boundary condition
|
||||
bool IsMinClosed() const { return _min.closed; }
|
||||
|
||||
/// Maximum boundary condition
|
||||
bool IsMaxClosed() const { return _max.closed; }
|
||||
|
||||
/// Minimum boundary condition
|
||||
bool IsMinOpen() const { return ! _min.closed; }
|
||||
|
||||
/// Maximum boundary condition
|
||||
bool IsMaxOpen() const { return ! _max.closed; }
|
||||
|
||||
/// Returns true if the maximum value is finite.
|
||||
bool IsMaxFinite() const {
|
||||
return (_max.value != -std::numeric_limits<double>::infinity()
|
||||
&& _max.value != std::numeric_limits<double>::infinity());
|
||||
}
|
||||
|
||||
/// Returns true if the minimum value is finite.
|
||||
bool IsMinFinite() const {
|
||||
return (_min.value != -std::numeric_limits<double>::infinity()
|
||||
&& _min.value != std::numeric_limits<double>::infinity());
|
||||
}
|
||||
|
||||
/// Returns true if both the maximum and minimum value are finite.
|
||||
bool IsFinite() const {
|
||||
return IsMaxFinite() && IsMinFinite();
|
||||
}
|
||||
|
||||
/// Return true iff the interval is empty.
|
||||
bool IsEmpty() const {
|
||||
return (_min.value > _max.value) ||
|
||||
((_min.value == _max.value)
|
||||
&& (! _min.closed || !_max.closed));
|
||||
}
|
||||
|
||||
/// Width of the interval.
|
||||
/// An empty interval has size 0.
|
||||
double GetSize() const {
|
||||
return GfMax( 0.0, _max.value - _min.value );
|
||||
}
|
||||
|
||||
// For 2x compatibility
|
||||
double Size() const { return GetSize(); }
|
||||
|
||||
/// Return true iff the value d is contained in the interval.
|
||||
/// An empty interval contains no values.
|
||||
bool Contains(double d) const {
|
||||
return ((d > _min.value) || (d == _min.value && _min.closed))
|
||||
&& ((d < _max.value) || (d == _max.value && _max.closed));
|
||||
}
|
||||
|
||||
// For 2x compatibility
|
||||
bool In(double d) const { return Contains(d); }
|
||||
|
||||
/// Return true iff the interval i is entirely contained in the interval.
|
||||
/// An empty interval contains no intervals, not even other
|
||||
/// empty intervals.
|
||||
bool Contains(const GfInterval &i) const {
|
||||
return (*this & i) == i;
|
||||
}
|
||||
|
||||
/// Return true iff the given interval i intersects this interval.
|
||||
bool Intersects(const GfInterval &i) const {
|
||||
return !(*this & i).IsEmpty();
|
||||
}
|
||||
|
||||
/// \name Math operations
|
||||
///@{
|
||||
|
||||
/// Boolean intersection.
|
||||
GfInterval & operator&=(const GfInterval &rhs) {
|
||||
if (IsEmpty()) {
|
||||
// No change
|
||||
} else if (rhs.IsEmpty()) {
|
||||
// Intersection is empty
|
||||
*this = GfInterval();
|
||||
} else {
|
||||
// Intersect min edge
|
||||
if (_min.value < rhs._min.value)
|
||||
_min = rhs._min;
|
||||
else if (_min.value == rhs._min.value)
|
||||
_min.closed &= rhs._min.closed;
|
||||
|
||||
// Intersect max edge
|
||||
if (_max.value > rhs._max.value)
|
||||
_max = rhs._max;
|
||||
else if (_max.value == rhs._max.value)
|
||||
_max.closed &= rhs._max.closed;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the interval that bounds the union of this interval and rhs.
|
||||
GfInterval & operator|=(const GfInterval &rhs) {
|
||||
if (IsEmpty()) {
|
||||
*this = rhs;
|
||||
} else if (rhs.IsEmpty()) {
|
||||
// No change
|
||||
} else {
|
||||
// Expand min edge
|
||||
if (_min.value > rhs._min.value)
|
||||
_min = rhs._min;
|
||||
else if (_min.value == rhs._min.value)
|
||||
_min.closed |= rhs._min.closed;
|
||||
|
||||
// Expand max edge
|
||||
if (_max.value < rhs._max.value)
|
||||
_max = rhs._max;
|
||||
else if (_max.value == rhs._max.value)
|
||||
_max.closed |= rhs._max.closed;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Interval addition.
|
||||
GfInterval & operator+=(const GfInterval &rhs) {
|
||||
if (!rhs.IsEmpty()) {
|
||||
_min.value += rhs._min.value;
|
||||
_max.value += rhs._max.value;
|
||||
_min.closed &= rhs._min.closed;
|
||||
_max.closed &= rhs._max.closed;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Interval subtraction.
|
||||
GfInterval & operator-=(const GfInterval &rhs) {
|
||||
return *this += -rhs;
|
||||
}
|
||||
|
||||
/// Interval unary minus.
|
||||
GfInterval operator-() const {
|
||||
return GfInterval(-_max.value, -_min.value, _max.closed, _min.closed);
|
||||
}
|
||||
|
||||
/// Interval multiplication.
|
||||
GfInterval & operator*=(const GfInterval &rhs) {
|
||||
const _Bound a = _min * rhs._min;
|
||||
const _Bound b = _min * rhs._max;
|
||||
const _Bound c = _max * rhs._min;
|
||||
const _Bound d = _max * rhs._max;
|
||||
_max = _Max( _Max(a,b), _Max(c,d) );
|
||||
_min = _Min( _Min(a,b), _Min(c,d) );
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Greater than operator
|
||||
bool operator>(const GfInterval& rhs) {
|
||||
// Defined in terms of operator<()
|
||||
return rhs < *this;
|
||||
}
|
||||
|
||||
/// Less than or equal operator
|
||||
bool operator<=(const GfInterval& rhs) {
|
||||
// Defined in terms of operator<()
|
||||
return !(rhs < *this);
|
||||
}
|
||||
|
||||
/// Greater than or equal operator
|
||||
bool operator>=(const GfInterval& rhs) {
|
||||
// Defined in terms of operator<()
|
||||
return !(*this < rhs);
|
||||
}
|
||||
|
||||
/// Union operator
|
||||
GfInterval operator|(const GfInterval& rhs) const {
|
||||
// Defined in terms of operator |=()
|
||||
GfInterval tmp(*this);
|
||||
tmp |= rhs;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Intersection operator
|
||||
GfInterval operator&(const GfInterval& rhs) const {
|
||||
// Defined in terms of operator &=()
|
||||
GfInterval tmp(*this);
|
||||
tmp &= rhs;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Addition operator
|
||||
GfInterval operator+(const GfInterval& rhs) const {
|
||||
// Defined in terms of operator +=()
|
||||
GfInterval tmp(*this);
|
||||
tmp += rhs;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtraction operator
|
||||
GfInterval operator-(const GfInterval& rhs) const {
|
||||
// Defined in terms of operator -=()
|
||||
GfInterval tmp(*this);
|
||||
tmp -= rhs;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplication operator
|
||||
GfInterval operator*(const GfInterval& rhs) const {
|
||||
// Defined in terms of operator *=()
|
||||
GfInterval tmp(*this);
|
||||
tmp *= rhs;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
///@}
|
||||
|
||||
/// Returns the full interval (-inf, inf).
|
||||
static GfInterval GetFullInterval() {
|
||||
return GfInterval( -std::numeric_limits<double>::infinity(),
|
||||
std::numeric_limits<double>::infinity(),
|
||||
false, false );
|
||||
}
|
||||
|
||||
private:
|
||||
// Helper struct to represent interval boundaries.
|
||||
struct _Bound {
|
||||
// Boundary value.
|
||||
double value;
|
||||
// Boundary condition. The boundary value is included in interval
|
||||
// only if the boundary is closed.
|
||||
bool closed;
|
||||
|
||||
_Bound(double val, bool isClosed) :
|
||||
value(val),
|
||||
closed(isClosed)
|
||||
{
|
||||
// Closed boundaries on infinite values do not make sense so
|
||||
// force the bound to be open
|
||||
if (value == -std::numeric_limits<double>::infinity() ||
|
||||
value == std::numeric_limits<double>::infinity()) {
|
||||
closed = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const _Bound &rhs) const {
|
||||
return value == rhs.value && closed == rhs.closed;
|
||||
}
|
||||
|
||||
bool operator!=(const _Bound &rhs) const {
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
bool operator<(const _Bound &rhs) const {
|
||||
return value < rhs.value || (value == rhs.value && closed && !rhs.closed);
|
||||
}
|
||||
|
||||
_Bound & operator=(const _Bound &rhs) {
|
||||
value = rhs.value;
|
||||
closed = rhs.closed;
|
||||
return *this;
|
||||
}
|
||||
_Bound operator*(const _Bound &rhs) const {
|
||||
return _Bound( value * rhs.value, closed & rhs.closed );
|
||||
}
|
||||
friend inline size_t hash_value(const _Bound &b) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, b.value);
|
||||
boost::hash_combine(h, b.closed);
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
// Return the lesser minimum bound, handling boundary conditions.
|
||||
inline static const _Bound &
|
||||
_Min( const _Bound &a, const _Bound &b ) {
|
||||
return (a.value < b.value
|
||||
|| ((a.value == b.value) && a.closed && !b.closed)) ?
|
||||
a : b;
|
||||
}
|
||||
|
||||
// Return the greater maximum bound, handling boundary conditions.
|
||||
inline static const _Bound &
|
||||
_Max( const _Bound &a, const _Bound &b ) {
|
||||
return (a.value < b.value
|
||||
|| ((a.value == b.value) && !a.closed && b.closed)) ?
|
||||
b : a;
|
||||
}
|
||||
|
||||
/// Data
|
||||
_Bound _min, _max;
|
||||
};
|
||||
|
||||
/// Output a GfInterval using the format (x, y).
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream &operator<<(std::ostream&, const GfInterval&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_INTERVAL_H
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_LIMITS_H
|
||||
#define PXR_BASE_GF_LIMITS_H
|
||||
|
||||
/// \file gf/limits.h
|
||||
/// \ingroup group_gf_BasicMath
|
||||
/// Defines useful mathematical limits.
|
||||
|
||||
/// This constant is used to determine whether the length of a vector is too
|
||||
/// small to handle accurately.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
#define GF_MIN_VECTOR_LENGTH 1e-10
|
||||
|
||||
/// This constant is used to determine when a set of basis vectors is close to
|
||||
/// orthogonal.
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
#define GF_MIN_ORTHO_TOLERANCE 1e-6
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,123 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_LINE_H
|
||||
#define PXR_BASE_GF_LINE_H
|
||||
|
||||
/// \file gf/line.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
|
||||
#include <float.h>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfLine
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 3D line
|
||||
///
|
||||
/// This class represents a three-dimensional line in space. Lines are
|
||||
/// constructed from a point, \p p0, and a direction, dir. The direction is
|
||||
/// normalized in the constructor.
|
||||
///
|
||||
/// The line is kept in a parametric represention, p = p0 + t * dir.
|
||||
///
|
||||
class GfLine {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves line parameters undefined.
|
||||
GfLine() {
|
||||
}
|
||||
|
||||
/// Construct a line from a point and a direction.
|
||||
GfLine(const GfVec3d &p0, const GfVec3d &dir ) {
|
||||
Set( p0, dir );
|
||||
}
|
||||
|
||||
double Set(const GfVec3d &p0, const GfVec3d &dir ) {
|
||||
_p0 = p0;
|
||||
_dir = dir;
|
||||
return _dir.Normalize();
|
||||
}
|
||||
|
||||
/// Return the point on the line at \p ( p0 + t * dir ).
|
||||
/// Remember dir has been normalized so t represents a unit distance.
|
||||
GfVec3d GetPoint( double t ) const { return _p0 + _dir * t; }
|
||||
|
||||
/// Return the normalized direction of the line.
|
||||
const GfVec3d &GetDirection() const { return _dir; }
|
||||
|
||||
/// Returns the point on the line that is closest to \p point. If \p t is
|
||||
/// not \c NULL, it will be set to the parametric distance along the line
|
||||
/// of the returned point.
|
||||
GF_API
|
||||
GfVec3d FindClosestPoint(const GfVec3d &point, double *t = NULL) const;
|
||||
|
||||
/// Component-wise equality test. The starting points and directions,
|
||||
/// must match exactly for lines to be considered equal.
|
||||
bool operator ==(const GfLine &l) const {
|
||||
return _p0 == l._p0 && _dir == l._dir;
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The starting points, and directions
|
||||
/// must match exactly for lines to be considered equal.
|
||||
bool operator !=(const GfLine &r) const {
|
||||
return ! (*this == r);
|
||||
}
|
||||
|
||||
private:
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfLine &, const GfLine &,
|
||||
GfVec3d *, GfVec3d *,
|
||||
double *, double * );
|
||||
// Parametric description:
|
||||
// l(t) = _p0 + t * _length * _dir;
|
||||
GfVec3d _p0;
|
||||
GfVec3d _dir;
|
||||
};
|
||||
|
||||
/// Computes the closets points between two lines.
|
||||
///
|
||||
/// The two points are returned in \p p1 and \p p2. The parametric distance
|
||||
/// of each point on the lines is returned in \p t1 and \p t2.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints(const GfLine &l1, const GfLine &l2,
|
||||
GfVec3d *p1 = nullptr, GfVec3d *p2 = nullptr,
|
||||
double *t1 = nullptr, double *t2 = nullptr);
|
||||
|
||||
/// Output a GfLine.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream &operator<<(std::ostream&, const GfLine&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_LINE_H
|
||||
@@ -0,0 +1,120 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_LINE2D_H
|
||||
#define PXR_BASE_GF_LINE2D_H
|
||||
|
||||
/// \file gf/line2d.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfLine2d
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 2D line
|
||||
///
|
||||
/// This class represents a two-dimensional line in space. Lines are
|
||||
/// constructed from a point, \p p0, and a direction, dir. The direction is
|
||||
/// normalized in the constructor.
|
||||
///
|
||||
/// The line is kept in a parametric represention, p = p0 + t * dir.
|
||||
///
|
||||
class GfLine2d {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves line parameters undefined.
|
||||
GfLine2d() {
|
||||
}
|
||||
|
||||
/// Construct a line from a point and a direction.
|
||||
GfLine2d(const GfVec2d &p0, const GfVec2d &dir ) {
|
||||
Set( p0, dir );
|
||||
}
|
||||
|
||||
double Set(const GfVec2d &p0, const GfVec2d &dir ) {
|
||||
_p0 = p0;
|
||||
_dir = dir;
|
||||
return _dir.Normalize();
|
||||
}
|
||||
|
||||
/// Return the point on the line at \p ( p0 + t * dir ).
|
||||
/// Remember dir has been normalized so t represents a unit distance.
|
||||
GfVec2d GetPoint( double t ) const { return _p0 + _dir * t; }
|
||||
|
||||
/// Return the normalized direction of the line.
|
||||
const GfVec2d &GetDirection() const { return _dir; }
|
||||
|
||||
/// Returns the point on the line that is closest to \p point. If \p t is
|
||||
/// not \c NULL, it will be set to the parametric distance along the line
|
||||
/// of the returned point.
|
||||
GF_API
|
||||
GfVec2d FindClosestPoint(const GfVec2d &point, double *t = NULL) const;
|
||||
|
||||
/// Component-wise equality test. The starting points and directions, must
|
||||
/// match exactly for lines to be considered equal.
|
||||
bool operator ==(const GfLine2d &l) const {
|
||||
return _p0 == l._p0 && _dir == l._dir;
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The starting points, and directions
|
||||
/// must match exactly for lines to be considered equal.
|
||||
bool operator !=(const GfLine2d &r) const {
|
||||
return ! (*this == r);
|
||||
}
|
||||
|
||||
private:
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfLine2d &, const GfLine2d &,
|
||||
GfVec2d *, GfVec2d *,
|
||||
double *, double *);
|
||||
|
||||
// Parametric description:
|
||||
// l(t) = _p0 + t * _length * _dir;
|
||||
GfVec2d _p0;
|
||||
GfVec2d _dir;
|
||||
};
|
||||
|
||||
/// Computes the closets points between two lines.
|
||||
///
|
||||
/// The two points are returned in \p p1 and \p p2. The parametric distance
|
||||
/// of each point on the lines is returned in \p t1 and \p t2.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints(const GfLine2d &l1, const GfLine2d &l2,
|
||||
GfVec2d *p1 = nullptr, GfVec2d *p2 = nullptr,
|
||||
double *t1 = nullptr, double *t2 = nullptr);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_LINE2D_H
|
||||
@@ -0,0 +1,135 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_LINE_SEG_H
|
||||
#define PXR_BASE_GF_LINE_SEG_H
|
||||
|
||||
/// \file gf/lineSeg.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/line.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <float.h>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfLineSeg
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 3D line segment
|
||||
///
|
||||
/// This class represents a three-dimensional line segment in space.
|
||||
///
|
||||
class GfLineSeg {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves line parameters undefined.
|
||||
GfLineSeg() {
|
||||
}
|
||||
|
||||
/// Construct a line segment that spans two points.
|
||||
GfLineSeg(const GfVec3d &p0, const GfVec3d &p1 ) {
|
||||
_length = _line.Set( p0, p1 - p0 );
|
||||
}
|
||||
|
||||
/// Return the point on the segment specified by the parameter t.
|
||||
/// p = p0 + t * (p1 - p0)
|
||||
GfVec3d GetPoint( double t ) const {return _line.GetPoint( t * _length );}
|
||||
|
||||
/// Return the normalized direction of the line.
|
||||
const GfVec3d &GetDirection() const { return _line.GetDirection(); }
|
||||
|
||||
/// Return the length of the line
|
||||
double GetLength() const { return _length; }
|
||||
|
||||
/// Returns the point on the line that is closest to \p point. If
|
||||
/// \p t is not \c NULL, it will be set to the parametric
|
||||
/// distance along the line of the closest point.
|
||||
GF_API
|
||||
GfVec3d FindClosestPoint(const GfVec3d &point, double *t = NULL) const;
|
||||
|
||||
/// Component-wise equality test. The starting points and directions,
|
||||
/// must match exactly for lines to be considered equal.
|
||||
bool operator ==(const GfLineSeg &l) const {
|
||||
return (_line == l._line && _length == l._length);
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The starting points,
|
||||
/// and directions must match exactly for lines to be
|
||||
/// considered equal.
|
||||
bool operator !=(const GfLineSeg &r) const {
|
||||
return ! (*this == r);
|
||||
}
|
||||
|
||||
private:
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfLine &, const GfLineSeg &,
|
||||
GfVec3d *, GfVec3d *,
|
||||
double *, double * );
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfLineSeg &, const GfLineSeg &,
|
||||
GfVec3d *, GfVec3d *,
|
||||
double *, double * );
|
||||
|
||||
GfLine _line;
|
||||
double _length; // distance from p0 to p1
|
||||
};
|
||||
|
||||
/// Computes the closets points on \p line and \p seg.
|
||||
///
|
||||
/// The two points are returned in \p p1 and \p p2. The parametric distances
|
||||
/// of \p p1 and \p p2 along the line and segment are returned in \p t1 and \p
|
||||
/// t2.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints( const GfLine &line, const GfLineSeg &seg,
|
||||
GfVec3d *p1 = nullptr, GfVec3d *p2 = nullptr,
|
||||
double *t1 = nullptr, double *t2 = nullptr );
|
||||
|
||||
/// Computes the closets points on two line segments, \p seg1 and \p seg2. The
|
||||
/// two points are returned in \p p1 and \p p2. The parametric distances of \p
|
||||
/// p1 and \p p2 along the segments are returned in \p t1 and \p t2.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints( const GfLineSeg &seg1, const GfLineSeg &seg2,
|
||||
GfVec3d *p1 = nullptr, GfVec3d *p2 = nullptr,
|
||||
double *t1 = nullptr, double *t2 = nullptr );
|
||||
|
||||
/// Output a GfLineSeg.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream &operator<<(std::ostream&, const GfLineSeg&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_LINE_SEG_H
|
||||
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_LINE_SEG2D_H
|
||||
#define PXR_BASE_GF_LINE_SEG2D_H
|
||||
|
||||
/// \file gf/lineSeg2d.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/line2d.h"
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfLineSeg2d
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 2D line segment
|
||||
///
|
||||
/// This class represents a three-dimensional line segment in space.
|
||||
///
|
||||
class GfLineSeg2d {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves line parameters undefined.
|
||||
GfLineSeg2d() {
|
||||
}
|
||||
|
||||
/// Construct a line segment that spans two points.
|
||||
GfLineSeg2d(const GfVec2d &p0, const GfVec2d &p1 ) {
|
||||
_length = _line.Set( p0, p1 - p0 );
|
||||
}
|
||||
|
||||
/// Return the point on the segment specified by the parameter t.
|
||||
/// p = p0 + t * (p1 - p0)
|
||||
GfVec2d GetPoint( double t ) const {return _line.GetPoint( t * _length );}
|
||||
|
||||
/// Return the normalized direction of the line.
|
||||
const GfVec2d &GetDirection() const { return _line.GetDirection(); }
|
||||
|
||||
/// Return the length of the line
|
||||
double GetLength() const { return _length; }
|
||||
|
||||
/// Returns the point on the line that is closest to \p point. If
|
||||
/// \p t is not \c NULL, it will be set to the parametric
|
||||
/// distance along the line of the closest point.
|
||||
GF_API
|
||||
GfVec2d FindClosestPoint(const GfVec2d &point, double *t = NULL) const;
|
||||
|
||||
/// Component-wise equality test. The starting points and directions,
|
||||
/// must match exactly for lines to be considered equal.
|
||||
bool operator ==(const GfLineSeg2d &l) const {
|
||||
return (_line == l._line && _length == l._length);
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The starting points,
|
||||
/// and directions must match exactly for lines to be
|
||||
/// considered equal.
|
||||
bool operator !=(const GfLineSeg2d &r) const {
|
||||
return ! (*this == r);
|
||||
}
|
||||
|
||||
private:
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfLine2d &, const GfLineSeg2d &,
|
||||
GfVec2d *, GfVec2d *,
|
||||
double *t1, double *t2 );
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfLineSeg2d &, const GfLineSeg2d &,
|
||||
GfVec2d *, GfVec2d *,
|
||||
double *, double * );
|
||||
|
||||
GfLine2d _line;
|
||||
double _length; // distance from p0 to p1
|
||||
};
|
||||
|
||||
/// Computes the closets points on \p line and \p seg.
|
||||
///
|
||||
/// The two points are returned in \p p1 and \p p2. The parametric distances
|
||||
/// of \p p1 and \p p2 along the line and segment are returned in \p t1 and \p
|
||||
/// t2.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints( const GfLine2d &line, const GfLineSeg2d &seg,
|
||||
GfVec2d *p1 = nullptr, GfVec2d *p2 = nullptr,
|
||||
double *t1 = nullptr, double *t2 = nullptr );
|
||||
|
||||
/// Computes the closets points on two line segments, \p seg1 and \p seg2.
|
||||
///
|
||||
/// The two points are returned in \p p1 and \p p2. The parametric distances
|
||||
/// of \p p1 and \p p2 along the segments are returned in \p t1 and \p t2.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints( const GfLineSeg2d &seg1, const GfLineSeg2d &seg2,
|
||||
GfVec2d *p1 = nullptr, GfVec2d *p2 = nullptr,
|
||||
double *t1 = nullptr, double *t2 = nullptr );
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_LINE_SEG2D_H
|
||||
@@ -0,0 +1,271 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_MATH_H
|
||||
#define PXR_BASE_GF_MATH_H
|
||||
|
||||
/// \file gf/math.h
|
||||
/// \ingroup group_gf_BasicMath
|
||||
/// Assorted mathematical utility functions.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/math.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Returns true if \p a and \p b are with \p epsilon of each other.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline bool GfIsClose(double a, double b, double epsilon) {
|
||||
return fabs(a-b) < epsilon;
|
||||
}
|
||||
|
||||
/// Converts an angle in radians to degrees.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfRadiansToDegrees(double radians) {
|
||||
return radians * (180.0 / M_PI);
|
||||
}
|
||||
|
||||
/// Converts an angle in degrees to radians.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfDegreesToRadians(double degrees) {
|
||||
return degrees * (M_PI / 180.0);
|
||||
}
|
||||
|
||||
/// Returns the inner product of \c x with itself: specifically, \c x*x.
|
||||
/// Defined for \c int, \c float, \c double, and all \c GfVec types.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <class T>
|
||||
inline double GfSqr(const T& x) {
|
||||
return x * x;
|
||||
}
|
||||
|
||||
/// Return the signum of \p v (i.e. -1, 0, or 1).
|
||||
///
|
||||
/// The type \c T must implement the < and > operators; the function returns
|
||||
/// zero only if value neither positive, nor negative.
|
||||
///
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <typename T>
|
||||
inline T
|
||||
GfSgn(T v) {
|
||||
return (v < 0) ? -1 : ((v > 0) ? 1 : 0);
|
||||
}
|
||||
|
||||
/// Return sqrt(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfSqrt(double f) { return std::sqrt(f); }
|
||||
/// Return sqrt(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfSqrt(float f) { return std::sqrt(f); }
|
||||
|
||||
/// Return exp(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfExp(double f) { return std::exp(f); }
|
||||
/// Return exp(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfExp(float f) { return std::exp(f); }
|
||||
|
||||
/// Return log(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfLog(double f) { return std::log(f); }
|
||||
/// Return log(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfLog(float f) { return std::log(f); }
|
||||
|
||||
/// Return floor(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfFloor(double f) { return std::floor(f); }
|
||||
/// Return floor(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfFloor(float f) { return std::floor(f); }
|
||||
|
||||
/// Return ceil(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfCeil(double f) { return std::ceil(f); }
|
||||
/// Return ceil(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfCeil(float f) { return std::ceil(f); }
|
||||
|
||||
/// Return abs(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfAbs(double f) { return std::fabs(f); }
|
||||
/// Return abs(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfAbs(float f) { return std::fabs(f); }
|
||||
|
||||
/// Return round(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfRound(double f) { return std::rint(f); }
|
||||
/// Return round(\p f).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfRound(float f) { return std::rint(f); }
|
||||
|
||||
/// Return pow(\p f, \p p).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfPow(double f, double p) { return std::pow(f, p); }
|
||||
/// Return pow(\p f, \p p).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfPow(float f, float p) { return std::pow(f, p); }
|
||||
|
||||
/// Return sin(\p v).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfSin(double v) { return std::sin(v); }
|
||||
/// Return sin(\p v).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfSin(float v) { return std::sin(v); }
|
||||
/// Return cos(\p v).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfCos(double v) { return std::cos(v); }
|
||||
/// Return cos(\p v).
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfCos(float v) { return std::cos(v); }
|
||||
/// Return sin(\p v) in \p s and cos(\p v) in \p c.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline void GfSinCos(double v, double *s, double *c) { ArchSinCos(v, s, c); }
|
||||
/// Return sin(\p v) in \p s and cos(\p v) in \p c.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline void GfSinCos(float v, float *s, float *c) { ArchSinCosf(v, s, c); }
|
||||
|
||||
/// Return the resulting of clamping \p value to lie between
|
||||
/// \p min and \p max. This function is also defined for GfVecs.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline double GfClamp(double value, double min, double max) {
|
||||
if (value < min) return min;
|
||||
if (value > max) return max;
|
||||
return value;
|
||||
}
|
||||
|
||||
/// \overload
|
||||
/// \ingroup group_gf_BasicMath
|
||||
inline float GfClamp(float value, float min, float max) {
|
||||
if (value < min) return min;
|
||||
if (value > max) return max;
|
||||
return value;
|
||||
}
|
||||
|
||||
/// The mod function with "correct" behaviour for negative numbers.
|
||||
///
|
||||
/// If \p a = \c n \p b for some integer \p n, zero is returned.
|
||||
/// Otherwise, for positive \p a, the value returned is \c fmod(a,b),
|
||||
/// and for negative \p a, the value returned is \c fmod(a,b)+b.
|
||||
///
|
||||
/// \ingroup group_gf_BasicMath
|
||||
GF_API
|
||||
double GfMod(double a, double b);
|
||||
/// \overload
|
||||
// \ingroup group_gf_BasicMath
|
||||
GF_API
|
||||
float GfMod(float a, float b);
|
||||
|
||||
/// Linear interpolation function.
|
||||
///
|
||||
/// For any type that supports multiplication by a scalar and binary addition, returns
|
||||
/// \code
|
||||
/// (1-alpha) * a + alpha * b
|
||||
/// \endcode
|
||||
///
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <class T>
|
||||
inline T GfLerp( double alpha, const T& a, const T& b) {
|
||||
return (1-alpha)* a + alpha * b;
|
||||
}
|
||||
|
||||
/// Returns the smallest of the given \c values.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <class T>
|
||||
inline T GfMin(T a1, T a2) {
|
||||
return (a1 < a2 ? a1 : a2);
|
||||
}
|
||||
template <class T>
|
||||
inline T GfMin(T a1, T a2, T a3) {
|
||||
return GfMin(GfMin(a1, a2), a3);
|
||||
}
|
||||
template <class T>
|
||||
inline T GfMin(T a1, T a2, T a3, T a4) {
|
||||
return GfMin(GfMin(a1, a2, a3), a4);
|
||||
}
|
||||
template <class T>
|
||||
inline T GfMin(T a1, T a2, T a3, T a4, T a5) {
|
||||
return GfMin(GfMin(a1, a2, a3, a4), a5);
|
||||
}
|
||||
|
||||
/// Returns the largest of the given \c values.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <class T>
|
||||
inline T GfMax(T a1, T a2) {
|
||||
return (a1 < a2 ? a2 : a1);
|
||||
}
|
||||
template <class T>
|
||||
inline T GfMax(T a1, T a2, T a3) {
|
||||
return GfMax(GfMax(a1, a2), a3);
|
||||
}
|
||||
template <class T>
|
||||
inline T GfMax(T a1, T a2, T a3, T a4) {
|
||||
return GfMax(GfMax(a1, a2, a3), a4);
|
||||
}
|
||||
template <class T>
|
||||
inline T GfMax(T a1, T a2, T a3, T a4, T a5) {
|
||||
return GfMax(GfMax(a1, a2, a3, a4), a5);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
/// For scalar types, this is just the regular product.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <typename Left, typename Right,
|
||||
std::enable_if_t<GfIsArithmetic<Left>::value &&
|
||||
GfIsArithmetic<Right>::value, int> = 0>
|
||||
inline decltype(std::declval<Left>() * std::declval<Right>())
|
||||
GfDot(Left left, Right right) {
|
||||
return left * right;
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors.
|
||||
/// For scalar types, this is just the regular product.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <typename Left, typename Right,
|
||||
std::enable_if_t<GfIsArithmetic<Left>::value &&
|
||||
GfIsArithmetic<Right>::value, int> = 0>
|
||||
inline decltype(std::declval<Left>() * std::declval<Right>())
|
||||
GfCompMult(Left left, Right right) {
|
||||
return left * right;
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors.
|
||||
/// For scalar types, this is just the regular quotient.
|
||||
/// \ingroup group_gf_BasicMath
|
||||
template <typename Left, typename Right,
|
||||
std::enable_if_t<GfIsArithmetic<Left>::value &&
|
||||
GfIsArithmetic<Right>::value, int> = 0>
|
||||
inline decltype(std::declval<Left>() / std::declval<Right>())
|
||||
GfCompDiv(Left left, Right right) {
|
||||
return left / right;
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATH_H
|
||||
@@ -0,0 +1,382 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// matrix2.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_MATRIX2D_H
|
||||
#define PXR_BASE_GF_MATRIX2D_H
|
||||
|
||||
/// \file gf/matrix2d.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/matrixData.h"
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfMatrix<class GfMatrix2d> { static const bool value = true; };
|
||||
|
||||
class GfMatrix2d;
|
||||
class GfMatrix2f;
|
||||
|
||||
/// \class GfMatrix2d
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Stores a 2x2 matrix of \c double elements. A basic type.
|
||||
///
|
||||
/// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
|
||||
/// indexes the element in the \e i th row and the \e j th column.
|
||||
///
|
||||
class GfMatrix2d
|
||||
{
|
||||
public:
|
||||
typedef double ScalarType;
|
||||
|
||||
static const size_t numRows = 2;
|
||||
static const size_t numColumns = 2;
|
||||
|
||||
/// Default constructor. Leaves the matrix component values undefined.
|
||||
GfMatrix2d() = default;
|
||||
|
||||
/// Constructor. Initializes the matrix from 4 independent
|
||||
/// \c double values, specified in row-major order. For example,
|
||||
/// parameter \e m10 specifies the value in row 1 and column 0.
|
||||
GfMatrix2d(double m00, double m01,
|
||||
double m10, double m11) {
|
||||
Set(m00, m01,
|
||||
m10, m11);
|
||||
}
|
||||
|
||||
/// Constructor. Initializes the matrix from a 2x2 array
|
||||
/// of \c double values, specified in row-major order.
|
||||
GfMatrix2d(const double m[2][2]) {
|
||||
Set(m);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to \e s times the
|
||||
/// identity matrix.
|
||||
explicit GfMatrix2d(double s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// This explicit constructor initializes the matrix to \p s times
|
||||
/// the identity matrix.
|
||||
explicit GfMatrix2d(int s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to diagonal form,
|
||||
/// with the \e i th element on the diagonal set to <c>v[i]</c>.
|
||||
explicit GfMatrix2d(const GfVec2d& v) {
|
||||
SetDiagonal(v);
|
||||
}
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// double. The vector is expected to be 2x2. If it is
|
||||
/// too big, only the first 2 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix2d(const std::vector< std::vector<double> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// float. The vector is expected to be 2x2. If it is
|
||||
/// too big, only the first 2 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix2d(const std::vector< std::vector<float> >& v);
|
||||
|
||||
/// This explicit constructor converts a "float" matrix to a "double" matrix.
|
||||
GF_API
|
||||
explicit GfMatrix2d(const class GfMatrix2f& m);
|
||||
|
||||
/// Sets a row of the matrix from a Vec2.
|
||||
void SetRow(int i, const GfVec2d & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
}
|
||||
|
||||
/// Sets a column of the matrix from a Vec2.
|
||||
void SetColumn(int i, const GfVec2d & v) {
|
||||
_mtx[0][i] = v[0];
|
||||
_mtx[1][i] = v[1];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec2.
|
||||
GfVec2d GetRow(int i) const {
|
||||
return GfVec2d(_mtx[i][0], _mtx[i][1]);
|
||||
}
|
||||
|
||||
/// Gets a column of the matrix as a Vec2.
|
||||
GfVec2d GetColumn(int i) const {
|
||||
return GfVec2d(_mtx[0][i], _mtx[1][i]);
|
||||
}
|
||||
|
||||
/// Sets the matrix from 4 independent \c double values,
|
||||
/// specified in row-major order. For example, parameter \e m10 specifies
|
||||
/// the value in row 1 and column 0.
|
||||
GfMatrix2d& Set(double m00, double m01,
|
||||
double m10, double m11) {
|
||||
_mtx[0][0] = m00; _mtx[0][1] = m01;
|
||||
_mtx[1][0] = m10; _mtx[1][1] = m11;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix from a 2x2 array of \c double
|
||||
/// values, specified in row-major order.
|
||||
GfMatrix2d& Set(const double m[2][2]) {
|
||||
_mtx[0][0] = m[0][0];
|
||||
_mtx[0][1] = m[0][1];
|
||||
_mtx[1][0] = m[1][0];
|
||||
_mtx[1][1] = m[1][1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix to the identity matrix.
|
||||
GfMatrix2d& SetIdentity() {
|
||||
return SetDiagonal(1);
|
||||
}
|
||||
|
||||
/// Sets the matrix to zero.
|
||||
GfMatrix2d& SetZero() {
|
||||
return SetDiagonal(0);
|
||||
}
|
||||
|
||||
/// Sets the matrix to \e s times the identity matrix.
|
||||
GF_API
|
||||
GfMatrix2d& SetDiagonal(double s);
|
||||
|
||||
/// Sets the matrix to have diagonal (<c>v[0], v[1]</c>).
|
||||
GF_API
|
||||
GfMatrix2d& SetDiagonal(const GfVec2d&);
|
||||
|
||||
/// Fills a 2x2 array of \c double values with the values in
|
||||
/// the matrix, specified in row-major order.
|
||||
GF_API
|
||||
double* Get(double m[2][2]) const;
|
||||
|
||||
/// Returns raw access to components of matrix as an array of
|
||||
/// \c double values. Components are in row-major order.
|
||||
double* data() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns const raw access to components of matrix as an array of
|
||||
/// \c double values. Components are in row-major order.
|
||||
const double* data() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as an array of \c double values.
|
||||
double* GetArray() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as a const array of \c double values.
|
||||
const double* GetArray() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 2 \c
|
||||
/// double values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
double* operator [](int i) { return _mtx[i]; }
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 2 \c
|
||||
/// double values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
const double* operator [](int i) const { return _mtx[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfMatrix2d const &m) {
|
||||
int nElems = 2 * 2;
|
||||
size_t h = 0;
|
||||
const double *p = m.GetArray();
|
||||
while (nElems--)
|
||||
boost::hash_combine(h, *p++);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix2d& m) const;
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix2f& m) const;
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix2d& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix2f& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Returns the transpose of the matrix.
|
||||
GF_API
|
||||
GfMatrix2d GetTranspose() const;
|
||||
|
||||
/// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
|
||||
/// matrix is singular. (FLT_MAX is the largest value a \c float can have,
|
||||
/// as defined by the system.) The matrix is considered singular if the
|
||||
/// determinant is less than or equal to the optional parameter \e eps. If
|
||||
/// \e det is non-null, <c>*det</c> is set to the determinant.
|
||||
GF_API
|
||||
GfMatrix2d GetInverse(double* det = NULL, double eps = 0) const;
|
||||
|
||||
/// Returns the determinant of the matrix.
|
||||
GF_API
|
||||
double GetDeterminant() const;
|
||||
|
||||
|
||||
/// Post-multiplies matrix \e m into this matrix.
|
||||
GF_API
|
||||
GfMatrix2d& operator *=(const GfMatrix2d& m);
|
||||
|
||||
/// Multiplies the matrix by a double.
|
||||
GF_API
|
||||
GfMatrix2d& operator *=(double);
|
||||
|
||||
/// Returns the product of a matrix and a double.
|
||||
friend GfMatrix2d operator *(const GfMatrix2d& m1, double d)
|
||||
{
|
||||
GfMatrix2d m = m1;
|
||||
return m *= d;
|
||||
}
|
||||
|
||||
///
|
||||
// Returns the product of a matrix and a double.
|
||||
friend GfMatrix2d operator *(double d, const GfMatrix2d& m)
|
||||
{
|
||||
return m * d;
|
||||
}
|
||||
|
||||
/// Adds matrix \e m to this matrix.
|
||||
GF_API
|
||||
GfMatrix2d& operator +=(const GfMatrix2d& m);
|
||||
|
||||
/// Subtracts matrix \e m from this matrix.
|
||||
GF_API
|
||||
GfMatrix2d& operator -=(const GfMatrix2d& m);
|
||||
|
||||
/// Returns the unary negation of matrix \e m.
|
||||
GF_API
|
||||
friend GfMatrix2d operator -(const GfMatrix2d& m);
|
||||
|
||||
/// Adds matrix \e m2 to \e m1
|
||||
friend GfMatrix2d operator +(const GfMatrix2d& m1, const GfMatrix2d& m2)
|
||||
{
|
||||
GfMatrix2d tmp(m1);
|
||||
tmp += m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtracts matrix \e m2 from \e m1.
|
||||
friend GfMatrix2d operator -(const GfMatrix2d& m1, const GfMatrix2d& m2)
|
||||
{
|
||||
GfMatrix2d tmp(m1);
|
||||
tmp -= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplies matrix \e m1 by \e m2.
|
||||
friend GfMatrix2d operator *(const GfMatrix2d& m1, const GfMatrix2d& m2)
|
||||
{
|
||||
GfMatrix2d tmp(m1);
|
||||
tmp *= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
|
||||
friend GfMatrix2d operator /(const GfMatrix2d& m1, const GfMatrix2d& m2)
|
||||
{
|
||||
return(m1 * m2.GetInverse());
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
friend inline GfVec2d operator *(const GfMatrix2d& m, const GfVec2d& vec) {
|
||||
return GfVec2d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1],
|
||||
vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1]);
|
||||
}
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
friend inline GfVec2d operator *(const GfVec2d &vec, const GfMatrix2d& m) {
|
||||
return GfVec2d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0],
|
||||
vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1]);
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
/// Note that the return type is a \c GfVec2f.
|
||||
GF_API
|
||||
friend GfVec2f operator *(const GfMatrix2d& m, const GfVec2f& vec);
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
/// Note that the return type is a \c GfVec2f.
|
||||
GF_API
|
||||
friend GfVec2f operator *(const GfVec2f &vec, const GfMatrix2d& m);
|
||||
|
||||
|
||||
private:
|
||||
/// Matrix storage, in row-major order.
|
||||
GfMatrixData<double, 2, 2> _mtx;
|
||||
|
||||
// Friend declarations
|
||||
friend class GfMatrix2f;
|
||||
};
|
||||
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// difference between each component of the matrix is less than or equal
|
||||
/// to \p tolerance, or false otherwise.
|
||||
GF_API
|
||||
bool GfIsClose(GfMatrix2d const &m1, GfMatrix2d const &m2, double tolerance);
|
||||
|
||||
/// Output a GfMatrix2d
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfMatrix2d const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX2D_H
|
||||
@@ -0,0 +1,372 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// matrix2.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_MATRIX2F_H
|
||||
#define PXR_BASE_GF_MATRIX2F_H
|
||||
|
||||
/// \file gf/matrix2f.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/matrixData.h"
|
||||
#include "pxr/base/gf/vec2f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfMatrix<class GfMatrix2f> { static const bool value = true; };
|
||||
|
||||
class GfMatrix2d;
|
||||
class GfMatrix2f;
|
||||
|
||||
/// \class GfMatrix2f
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Stores a 2x2 matrix of \c float elements. A basic type.
|
||||
///
|
||||
/// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
|
||||
/// indexes the element in the \e i th row and the \e j th column.
|
||||
///
|
||||
class GfMatrix2f
|
||||
{
|
||||
public:
|
||||
typedef float ScalarType;
|
||||
|
||||
static const size_t numRows = 2;
|
||||
static const size_t numColumns = 2;
|
||||
|
||||
/// Default constructor. Leaves the matrix component values undefined.
|
||||
GfMatrix2f() = default;
|
||||
|
||||
/// Constructor. Initializes the matrix from 4 independent
|
||||
/// \c float values, specified in row-major order. For example,
|
||||
/// parameter \e m10 specifies the value in row 1 and column 0.
|
||||
GfMatrix2f(float m00, float m01,
|
||||
float m10, float m11) {
|
||||
Set(m00, m01,
|
||||
m10, m11);
|
||||
}
|
||||
|
||||
/// Constructor. Initializes the matrix from a 2x2 array
|
||||
/// of \c float values, specified in row-major order.
|
||||
GfMatrix2f(const float m[2][2]) {
|
||||
Set(m);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to \e s times the
|
||||
/// identity matrix.
|
||||
explicit GfMatrix2f(float s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// This explicit constructor initializes the matrix to \p s times
|
||||
/// the identity matrix.
|
||||
explicit GfMatrix2f(int s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to diagonal form,
|
||||
/// with the \e i th element on the diagonal set to <c>v[i]</c>.
|
||||
explicit GfMatrix2f(const GfVec2f& v) {
|
||||
SetDiagonal(v);
|
||||
}
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// double. The vector is expected to be 2x2. If it is
|
||||
/// too big, only the first 2 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix2f(const std::vector< std::vector<double> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// float. The vector is expected to be 2x2. If it is
|
||||
/// too big, only the first 2 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix2f(const std::vector< std::vector<float> >& v);
|
||||
|
||||
/// This explicit constructor converts a "double" matrix to a "float" matrix.
|
||||
GF_API
|
||||
explicit GfMatrix2f(const class GfMatrix2d& m);
|
||||
|
||||
/// Sets a row of the matrix from a Vec2.
|
||||
void SetRow(int i, const GfVec2f & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
}
|
||||
|
||||
/// Sets a column of the matrix from a Vec2.
|
||||
void SetColumn(int i, const GfVec2f & v) {
|
||||
_mtx[0][i] = v[0];
|
||||
_mtx[1][i] = v[1];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec2.
|
||||
GfVec2f GetRow(int i) const {
|
||||
return GfVec2f(_mtx[i][0], _mtx[i][1]);
|
||||
}
|
||||
|
||||
/// Gets a column of the matrix as a Vec2.
|
||||
GfVec2f GetColumn(int i) const {
|
||||
return GfVec2f(_mtx[0][i], _mtx[1][i]);
|
||||
}
|
||||
|
||||
/// Sets the matrix from 4 independent \c float values,
|
||||
/// specified in row-major order. For example, parameter \e m10 specifies
|
||||
/// the value in row 1 and column 0.
|
||||
GfMatrix2f& Set(float m00, float m01,
|
||||
float m10, float m11) {
|
||||
_mtx[0][0] = m00; _mtx[0][1] = m01;
|
||||
_mtx[1][0] = m10; _mtx[1][1] = m11;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix from a 2x2 array of \c float
|
||||
/// values, specified in row-major order.
|
||||
GfMatrix2f& Set(const float m[2][2]) {
|
||||
_mtx[0][0] = m[0][0];
|
||||
_mtx[0][1] = m[0][1];
|
||||
_mtx[1][0] = m[1][0];
|
||||
_mtx[1][1] = m[1][1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix to the identity matrix.
|
||||
GfMatrix2f& SetIdentity() {
|
||||
return SetDiagonal(1);
|
||||
}
|
||||
|
||||
/// Sets the matrix to zero.
|
||||
GfMatrix2f& SetZero() {
|
||||
return SetDiagonal(0);
|
||||
}
|
||||
|
||||
/// Sets the matrix to \e s times the identity matrix.
|
||||
GF_API
|
||||
GfMatrix2f& SetDiagonal(float s);
|
||||
|
||||
/// Sets the matrix to have diagonal (<c>v[0], v[1]</c>).
|
||||
GF_API
|
||||
GfMatrix2f& SetDiagonal(const GfVec2f&);
|
||||
|
||||
/// Fills a 2x2 array of \c float values with the values in
|
||||
/// the matrix, specified in row-major order.
|
||||
GF_API
|
||||
float* Get(float m[2][2]) const;
|
||||
|
||||
/// Returns raw access to components of matrix as an array of
|
||||
/// \c float values. Components are in row-major order.
|
||||
float* data() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns const raw access to components of matrix as an array of
|
||||
/// \c float values. Components are in row-major order.
|
||||
const float* data() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as an array of \c float values.
|
||||
float* GetArray() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as a const array of \c float values.
|
||||
const float* GetArray() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 2 \c
|
||||
/// float values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
float* operator [](int i) { return _mtx[i]; }
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 2 \c
|
||||
/// float values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
const float* operator [](int i) const { return _mtx[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfMatrix2f const &m) {
|
||||
int nElems = 2 * 2;
|
||||
size_t h = 0;
|
||||
const float *p = m.GetArray();
|
||||
while (nElems--)
|
||||
boost::hash_combine(h, *p++);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix2d& m) const;
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix2f& m) const;
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix2d& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix2f& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Returns the transpose of the matrix.
|
||||
GF_API
|
||||
GfMatrix2f GetTranspose() const;
|
||||
|
||||
/// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
|
||||
/// matrix is singular. (FLT_MAX is the largest value a \c float can have,
|
||||
/// as defined by the system.) The matrix is considered singular if the
|
||||
/// determinant is less than or equal to the optional parameter \e eps. If
|
||||
/// \e det is non-null, <c>*det</c> is set to the determinant.
|
||||
GF_API
|
||||
GfMatrix2f GetInverse(double* det = NULL, double eps = 0) const;
|
||||
|
||||
/// Returns the determinant of the matrix.
|
||||
GF_API
|
||||
double GetDeterminant() const;
|
||||
|
||||
|
||||
/// Post-multiplies matrix \e m into this matrix.
|
||||
GF_API
|
||||
GfMatrix2f& operator *=(const GfMatrix2f& m);
|
||||
|
||||
/// Multiplies the matrix by a float.
|
||||
GF_API
|
||||
GfMatrix2f& operator *=(double);
|
||||
|
||||
/// Returns the product of a matrix and a float.
|
||||
friend GfMatrix2f operator *(const GfMatrix2f& m1, double d)
|
||||
{
|
||||
GfMatrix2f m = m1;
|
||||
return m *= d;
|
||||
}
|
||||
|
||||
///
|
||||
// Returns the product of a matrix and a float.
|
||||
friend GfMatrix2f operator *(double d, const GfMatrix2f& m)
|
||||
{
|
||||
return m * d;
|
||||
}
|
||||
|
||||
/// Adds matrix \e m to this matrix.
|
||||
GF_API
|
||||
GfMatrix2f& operator +=(const GfMatrix2f& m);
|
||||
|
||||
/// Subtracts matrix \e m from this matrix.
|
||||
GF_API
|
||||
GfMatrix2f& operator -=(const GfMatrix2f& m);
|
||||
|
||||
/// Returns the unary negation of matrix \e m.
|
||||
GF_API
|
||||
friend GfMatrix2f operator -(const GfMatrix2f& m);
|
||||
|
||||
/// Adds matrix \e m2 to \e m1
|
||||
friend GfMatrix2f operator +(const GfMatrix2f& m1, const GfMatrix2f& m2)
|
||||
{
|
||||
GfMatrix2f tmp(m1);
|
||||
tmp += m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtracts matrix \e m2 from \e m1.
|
||||
friend GfMatrix2f operator -(const GfMatrix2f& m1, const GfMatrix2f& m2)
|
||||
{
|
||||
GfMatrix2f tmp(m1);
|
||||
tmp -= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplies matrix \e m1 by \e m2.
|
||||
friend GfMatrix2f operator *(const GfMatrix2f& m1, const GfMatrix2f& m2)
|
||||
{
|
||||
GfMatrix2f tmp(m1);
|
||||
tmp *= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
|
||||
friend GfMatrix2f operator /(const GfMatrix2f& m1, const GfMatrix2f& m2)
|
||||
{
|
||||
return(m1 * m2.GetInverse());
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
friend inline GfVec2f operator *(const GfMatrix2f& m, const GfVec2f& vec) {
|
||||
return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1],
|
||||
vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1]);
|
||||
}
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
friend inline GfVec2f operator *(const GfVec2f &vec, const GfMatrix2f& m) {
|
||||
return GfVec2f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0],
|
||||
vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1]);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
/// Matrix storage, in row-major order.
|
||||
GfMatrixData<float, 2, 2> _mtx;
|
||||
|
||||
// Friend declarations
|
||||
friend class GfMatrix2d;
|
||||
};
|
||||
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// difference between each component of the matrix is less than or equal
|
||||
/// to \p tolerance, or false otherwise.
|
||||
GF_API
|
||||
bool GfIsClose(GfMatrix2f const &m1, GfMatrix2f const &m2, double tolerance);
|
||||
|
||||
/// Output a GfMatrix2f
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfMatrix2f const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX2F_H
|
||||
@@ -0,0 +1,509 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// matrix3.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_MATRIX3D_H
|
||||
#define PXR_BASE_GF_MATRIX3D_H
|
||||
|
||||
/// \file gf/matrix3d.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/matrixData.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfMatrix<class GfMatrix3d> { static const bool value = true; };
|
||||
|
||||
class GfMatrix3d;
|
||||
class GfMatrix3f;
|
||||
class GfRotation;
|
||||
class GfQuaternion;
|
||||
class GfQuatd;
|
||||
|
||||
/// \class GfMatrix3d
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Stores a 3x3 matrix of \c double elements. A basic type.
|
||||
///
|
||||
/// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
|
||||
/// indexes the element in the \e i th row and the \e j th column.
|
||||
///
|
||||
/// <h3>3D Transformations</h3>
|
||||
///
|
||||
/// Three methods, SetRotate(), SetScale(), and ExtractRotation(), interpret
|
||||
/// a GfMatrix3d as a 3D transformation. By convention, vectors are treated
|
||||
/// primarily as row vectors, implying the following:
|
||||
///
|
||||
/// \li Transformation matrices are organized to deal with row
|
||||
/// vectors, not column vectors.
|
||||
/// \li Each of the Set() methods in this class completely rewrites the
|
||||
/// matrix; for example, SetRotate() yields a matrix
|
||||
/// which does nothing but rotate.
|
||||
/// \li When multiplying two transformation matrices, the matrix
|
||||
/// on the left applies a more local transformation to a row
|
||||
/// vector. For example, if R represents a rotation
|
||||
/// matrix and S represents a scale matrix, the
|
||||
/// product R*S will rotate a row vector, then scale
|
||||
/// it.
|
||||
class GfMatrix3d
|
||||
{
|
||||
public:
|
||||
typedef double ScalarType;
|
||||
|
||||
static const size_t numRows = 3;
|
||||
static const size_t numColumns = 3;
|
||||
|
||||
/// Default constructor. Leaves the matrix component values undefined.
|
||||
GfMatrix3d() = default;
|
||||
|
||||
/// Constructor. Initializes the matrix from 9 independent
|
||||
/// \c double values, specified in row-major order. For example,
|
||||
/// parameter \e m10 specifies the value in row 1 and column 0.
|
||||
GfMatrix3d(double m00, double m01, double m02,
|
||||
double m10, double m11, double m12,
|
||||
double m20, double m21, double m22) {
|
||||
Set(m00, m01, m02,
|
||||
m10, m11, m12,
|
||||
m20, m21, m22);
|
||||
}
|
||||
|
||||
/// Constructor. Initializes the matrix from a 3x3 array
|
||||
/// of \c double values, specified in row-major order.
|
||||
GfMatrix3d(const double m[3][3]) {
|
||||
Set(m);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to \e s times the
|
||||
/// identity matrix.
|
||||
explicit GfMatrix3d(double s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// This explicit constructor initializes the matrix to \p s times
|
||||
/// the identity matrix.
|
||||
explicit GfMatrix3d(int s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to diagonal form,
|
||||
/// with the \e i th element on the diagonal set to <c>v[i]</c>.
|
||||
explicit GfMatrix3d(const GfVec3d& v) {
|
||||
SetDiagonal(v);
|
||||
}
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// double. The vector is expected to be 3x3. If it is
|
||||
/// too big, only the first 3 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix3d(const std::vector< std::vector<double> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// float. The vector is expected to be 3x3. If it is
|
||||
/// too big, only the first 3 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix3d(const std::vector< std::vector<float> >& v);
|
||||
|
||||
/// Constructor. Initialize matrix from rotation.
|
||||
GF_API
|
||||
GfMatrix3d(const GfRotation& rot);
|
||||
|
||||
/// Constructor. Initialize matrix from a quaternion.
|
||||
GF_API
|
||||
explicit GfMatrix3d(const GfQuatd& rot);
|
||||
|
||||
/// This explicit constructor converts a "float" matrix to a "double" matrix.
|
||||
GF_API
|
||||
explicit GfMatrix3d(const class GfMatrix3f& m);
|
||||
|
||||
/// Sets a row of the matrix from a Vec3.
|
||||
void SetRow(int i, const GfVec3d & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
_mtx[i][2] = v[2];
|
||||
}
|
||||
|
||||
/// Sets a column of the matrix from a Vec3.
|
||||
void SetColumn(int i, const GfVec3d & v) {
|
||||
_mtx[0][i] = v[0];
|
||||
_mtx[1][i] = v[1];
|
||||
_mtx[2][i] = v[2];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec3.
|
||||
GfVec3d GetRow(int i) const {
|
||||
return GfVec3d(_mtx[i][0], _mtx[i][1], _mtx[i][2]);
|
||||
}
|
||||
|
||||
/// Gets a column of the matrix as a Vec3.
|
||||
GfVec3d GetColumn(int i) const {
|
||||
return GfVec3d(_mtx[0][i], _mtx[1][i], _mtx[2][i]);
|
||||
}
|
||||
|
||||
/// Sets the matrix from 9 independent \c double values,
|
||||
/// specified in row-major order. For example, parameter \e m10 specifies
|
||||
/// the value in row 1 and column 0.
|
||||
GfMatrix3d& Set(double m00, double m01, double m02,
|
||||
double m10, double m11, double m12,
|
||||
double m20, double m21, double m22) {
|
||||
_mtx[0][0] = m00; _mtx[0][1] = m01; _mtx[0][2] = m02;
|
||||
_mtx[1][0] = m10; _mtx[1][1] = m11; _mtx[1][2] = m12;
|
||||
_mtx[2][0] = m20; _mtx[2][1] = m21; _mtx[2][2] = m22;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix from a 3x3 array of \c double
|
||||
/// values, specified in row-major order.
|
||||
GfMatrix3d& Set(const double m[3][3]) {
|
||||
_mtx[0][0] = m[0][0];
|
||||
_mtx[0][1] = m[0][1];
|
||||
_mtx[0][2] = m[0][2];
|
||||
_mtx[1][0] = m[1][0];
|
||||
_mtx[1][1] = m[1][1];
|
||||
_mtx[1][2] = m[1][2];
|
||||
_mtx[2][0] = m[2][0];
|
||||
_mtx[2][1] = m[2][1];
|
||||
_mtx[2][2] = m[2][2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix to the identity matrix.
|
||||
GfMatrix3d& SetIdentity() {
|
||||
return SetDiagonal(1);
|
||||
}
|
||||
|
||||
/// Sets the matrix to zero.
|
||||
GfMatrix3d& SetZero() {
|
||||
return SetDiagonal(0);
|
||||
}
|
||||
|
||||
/// Sets the matrix to \e s times the identity matrix.
|
||||
GF_API
|
||||
GfMatrix3d& SetDiagonal(double s);
|
||||
|
||||
/// Sets the matrix to have diagonal (<c>v[0], v[1], v[2]</c>).
|
||||
GF_API
|
||||
GfMatrix3d& SetDiagonal(const GfVec3d&);
|
||||
|
||||
/// Fills a 3x3 array of \c double values with the values in
|
||||
/// the matrix, specified in row-major order.
|
||||
GF_API
|
||||
double* Get(double m[3][3]) const;
|
||||
|
||||
/// Returns raw access to components of matrix as an array of
|
||||
/// \c double values. Components are in row-major order.
|
||||
double* data() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns const raw access to components of matrix as an array of
|
||||
/// \c double values. Components are in row-major order.
|
||||
const double* data() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as an array of \c double values.
|
||||
double* GetArray() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as a const array of \c double values.
|
||||
const double* GetArray() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 3 \c
|
||||
/// double values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
double* operator [](int i) { return _mtx[i]; }
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 3 \c
|
||||
/// double values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
const double* operator [](int i) const { return _mtx[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfMatrix3d const &m) {
|
||||
int nElems = 3 * 3;
|
||||
size_t h = 0;
|
||||
const double *p = m.GetArray();
|
||||
while (nElems--)
|
||||
boost::hash_combine(h, *p++);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix3d& m) const;
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix3f& m) const;
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix3d& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix3f& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Returns the transpose of the matrix.
|
||||
GF_API
|
||||
GfMatrix3d GetTranspose() const;
|
||||
|
||||
/// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
|
||||
/// matrix is singular. (FLT_MAX is the largest value a \c float can have,
|
||||
/// as defined by the system.) The matrix is considered singular if the
|
||||
/// determinant is less than or equal to the optional parameter \e eps. If
|
||||
/// \e det is non-null, <c>*det</c> is set to the determinant.
|
||||
GF_API
|
||||
GfMatrix3d GetInverse(double* det = NULL, double eps = 0) const;
|
||||
|
||||
/// Returns the determinant of the matrix.
|
||||
GF_API
|
||||
double GetDeterminant() const;
|
||||
|
||||
/// Makes the matrix orthonormal in place. This is an iterative method that
|
||||
/// is much more stable than the previous cross/cross method. If the
|
||||
/// iterative method does not converge, a warning is issued.
|
||||
///
|
||||
/// Returns true if the iteration converged, false otherwise. Leaves any
|
||||
/// translation part of the matrix unchanged. If \a issueWarning is true,
|
||||
/// this method will issue a warning if the iteration does not converge,
|
||||
/// otherwise it will be silent.
|
||||
GF_API
|
||||
bool Orthonormalize(bool issueWarning=true);
|
||||
|
||||
/// Returns an orthonormalized copy of the matrix.
|
||||
GF_API
|
||||
GfMatrix3d GetOrthonormalized(bool issueWarning=true) const;
|
||||
|
||||
/// Returns the sign of the determinant of the matrix, i.e. 1 for a
|
||||
/// right-handed matrix, -1 for a left-handed matrix, and 0 for a
|
||||
/// singular matrix.
|
||||
GF_API
|
||||
double GetHandedness() const;
|
||||
|
||||
/// Returns true if the vectors in the matrix form a right-handed
|
||||
/// coordinate system.
|
||||
bool IsRightHanded() const {
|
||||
return GetHandedness() == 1.0;
|
||||
}
|
||||
|
||||
/// Returns true if the vectors in matrix form a left-handed
|
||||
/// coordinate system.
|
||||
bool IsLeftHanded() const {
|
||||
return GetHandedness() == -1.0;
|
||||
}
|
||||
|
||||
/// Post-multiplies matrix \e m into this matrix.
|
||||
GF_API
|
||||
GfMatrix3d& operator *=(const GfMatrix3d& m);
|
||||
|
||||
/// Multiplies the matrix by a double.
|
||||
GF_API
|
||||
GfMatrix3d& operator *=(double);
|
||||
|
||||
/// Returns the product of a matrix and a double.
|
||||
friend GfMatrix3d operator *(const GfMatrix3d& m1, double d)
|
||||
{
|
||||
GfMatrix3d m = m1;
|
||||
return m *= d;
|
||||
}
|
||||
|
||||
///
|
||||
// Returns the product of a matrix and a double.
|
||||
friend GfMatrix3d operator *(double d, const GfMatrix3d& m)
|
||||
{
|
||||
return m * d;
|
||||
}
|
||||
|
||||
/// Adds matrix \e m to this matrix.
|
||||
GF_API
|
||||
GfMatrix3d& operator +=(const GfMatrix3d& m);
|
||||
|
||||
/// Subtracts matrix \e m from this matrix.
|
||||
GF_API
|
||||
GfMatrix3d& operator -=(const GfMatrix3d& m);
|
||||
|
||||
/// Returns the unary negation of matrix \e m.
|
||||
GF_API
|
||||
friend GfMatrix3d operator -(const GfMatrix3d& m);
|
||||
|
||||
/// Adds matrix \e m2 to \e m1
|
||||
friend GfMatrix3d operator +(const GfMatrix3d& m1, const GfMatrix3d& m2)
|
||||
{
|
||||
GfMatrix3d tmp(m1);
|
||||
tmp += m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtracts matrix \e m2 from \e m1.
|
||||
friend GfMatrix3d operator -(const GfMatrix3d& m1, const GfMatrix3d& m2)
|
||||
{
|
||||
GfMatrix3d tmp(m1);
|
||||
tmp -= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplies matrix \e m1 by \e m2.
|
||||
friend GfMatrix3d operator *(const GfMatrix3d& m1, const GfMatrix3d& m2)
|
||||
{
|
||||
GfMatrix3d tmp(m1);
|
||||
tmp *= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
|
||||
friend GfMatrix3d operator /(const GfMatrix3d& m1, const GfMatrix3d& m2)
|
||||
{
|
||||
return(m1 * m2.GetInverse());
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
friend inline GfVec3d operator *(const GfMatrix3d& m, const GfVec3d& vec) {
|
||||
return GfVec3d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1] + vec[2] * m._mtx[0][2],
|
||||
vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[1][2],
|
||||
vec[0] * m._mtx[2][0] + vec[1] * m._mtx[2][1] + vec[2] * m._mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
friend inline GfVec3d operator *(const GfVec3d &vec, const GfMatrix3d& m) {
|
||||
return GfVec3d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0] + vec[2] * m._mtx[2][0],
|
||||
vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[2][1],
|
||||
vec[0] * m._mtx[0][2] + vec[1] * m._mtx[1][2] + vec[2] * m._mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
/// Note that the return type is a \c GfVec3f.
|
||||
GF_API
|
||||
friend GfVec3f operator *(const GfMatrix3d& m, const GfVec3f& vec);
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
/// Note that the return type is a \c GfVec3f.
|
||||
GF_API
|
||||
friend GfVec3f operator *(const GfVec3f &vec, const GfMatrix3d& m);
|
||||
|
||||
/// Sets matrix to specify a uniform scaling by \e scaleFactor.
|
||||
GF_API
|
||||
GfMatrix3d& SetScale(double scaleFactor);
|
||||
|
||||
/// \name 3D Transformation Utilities
|
||||
/// @{
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot.
|
||||
GF_API
|
||||
GfMatrix3d& SetRotate(const GfQuatd &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot.
|
||||
GF_API
|
||||
GfMatrix3d& SetRotate(const GfRotation &rot);
|
||||
|
||||
/// Sets the matrix to specify a nonuniform scaling in x, y, and z by
|
||||
/// the factors in vector \e scaleFactors.
|
||||
GF_API
|
||||
GfMatrix3d& SetScale(const GfVec3d &scaleFactors);
|
||||
|
||||
/// Returns the rotation corresponding to this matrix. This works
|
||||
/// well only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfRotation ExtractRotation() const;
|
||||
|
||||
/// Decompose the rotation corresponding to this matrix about 3
|
||||
/// orthogonal axes. If the axes are not orthogonal, warnings
|
||||
/// will be spewed.
|
||||
///
|
||||
/// This is a convenience method that is equivalent to calling
|
||||
/// ExtractRotation().Decompose().
|
||||
GF_API
|
||||
GfVec3d DecomposeRotation(const GfVec3d &axis0,
|
||||
const GfVec3d &axis1,
|
||||
const GfVec3d &axis2 ) const;
|
||||
|
||||
/// Returns the quaternion corresponding to this matrix. This works
|
||||
/// well only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfQuaternion ExtractRotationQuaternion() const;
|
||||
|
||||
/// @}
|
||||
|
||||
private:
|
||||
/// Set the matrix to the rotation given by a quaternion,
|
||||
/// defined by the real component \p r and imaginary components \p i.
|
||||
void _SetRotateFromQuat(double r, const GfVec3d& i);
|
||||
|
||||
|
||||
private:
|
||||
/// Matrix storage, in row-major order.
|
||||
GfMatrixData<double, 3, 3> _mtx;
|
||||
|
||||
// Friend declarations
|
||||
friend class GfMatrix3f;
|
||||
};
|
||||
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// difference between each component of the matrix is less than or equal
|
||||
/// to \p tolerance, or false otherwise.
|
||||
GF_API
|
||||
bool GfIsClose(GfMatrix3d const &m1, GfMatrix3d const &m2, double tolerance);
|
||||
|
||||
/// Output a GfMatrix3d
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfMatrix3d const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX3D_H
|
||||
@@ -0,0 +1,499 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// matrix3.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_MATRIX3F_H
|
||||
#define PXR_BASE_GF_MATRIX3F_H
|
||||
|
||||
/// \file gf/matrix3f.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/matrixData.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfMatrix<class GfMatrix3f> { static const bool value = true; };
|
||||
|
||||
class GfMatrix3d;
|
||||
class GfMatrix3f;
|
||||
class GfRotation;
|
||||
class GfQuaternion;
|
||||
class GfQuatf;
|
||||
|
||||
/// \class GfMatrix3f
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Stores a 3x3 matrix of \c float elements. A basic type.
|
||||
///
|
||||
/// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
|
||||
/// indexes the element in the \e i th row and the \e j th column.
|
||||
///
|
||||
/// <h3>3D Transformations</h3>
|
||||
///
|
||||
/// Three methods, SetRotate(), SetScale(), and ExtractRotation(), interpret
|
||||
/// a GfMatrix3f as a 3D transformation. By convention, vectors are treated
|
||||
/// primarily as row vectors, implying the following:
|
||||
///
|
||||
/// \li Transformation matrices are organized to deal with row
|
||||
/// vectors, not column vectors.
|
||||
/// \li Each of the Set() methods in this class completely rewrites the
|
||||
/// matrix; for example, SetRotate() yields a matrix
|
||||
/// which does nothing but rotate.
|
||||
/// \li When multiplying two transformation matrices, the matrix
|
||||
/// on the left applies a more local transformation to a row
|
||||
/// vector. For example, if R represents a rotation
|
||||
/// matrix and S represents a scale matrix, the
|
||||
/// product R*S will rotate a row vector, then scale
|
||||
/// it.
|
||||
class GfMatrix3f
|
||||
{
|
||||
public:
|
||||
typedef float ScalarType;
|
||||
|
||||
static const size_t numRows = 3;
|
||||
static const size_t numColumns = 3;
|
||||
|
||||
/// Default constructor. Leaves the matrix component values undefined.
|
||||
GfMatrix3f() = default;
|
||||
|
||||
/// Constructor. Initializes the matrix from 9 independent
|
||||
/// \c float values, specified in row-major order. For example,
|
||||
/// parameter \e m10 specifies the value in row 1 and column 0.
|
||||
GfMatrix3f(float m00, float m01, float m02,
|
||||
float m10, float m11, float m12,
|
||||
float m20, float m21, float m22) {
|
||||
Set(m00, m01, m02,
|
||||
m10, m11, m12,
|
||||
m20, m21, m22);
|
||||
}
|
||||
|
||||
/// Constructor. Initializes the matrix from a 3x3 array
|
||||
/// of \c float values, specified in row-major order.
|
||||
GfMatrix3f(const float m[3][3]) {
|
||||
Set(m);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to \e s times the
|
||||
/// identity matrix.
|
||||
explicit GfMatrix3f(float s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// This explicit constructor initializes the matrix to \p s times
|
||||
/// the identity matrix.
|
||||
explicit GfMatrix3f(int s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to diagonal form,
|
||||
/// with the \e i th element on the diagonal set to <c>v[i]</c>.
|
||||
explicit GfMatrix3f(const GfVec3f& v) {
|
||||
SetDiagonal(v);
|
||||
}
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// double. The vector is expected to be 3x3. If it is
|
||||
/// too big, only the first 3 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix3f(const std::vector< std::vector<double> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// float. The vector is expected to be 3x3. If it is
|
||||
/// too big, only the first 3 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix3f(const std::vector< std::vector<float> >& v);
|
||||
|
||||
/// Constructor. Initialize matrix from rotation.
|
||||
GF_API
|
||||
GfMatrix3f(const GfRotation& rot);
|
||||
|
||||
/// Constructor. Initialize matrix from a quaternion.
|
||||
GF_API
|
||||
explicit GfMatrix3f(const GfQuatf& rot);
|
||||
|
||||
/// This explicit constructor converts a "double" matrix to a "float" matrix.
|
||||
GF_API
|
||||
explicit GfMatrix3f(const class GfMatrix3d& m);
|
||||
|
||||
/// Sets a row of the matrix from a Vec3.
|
||||
void SetRow(int i, const GfVec3f & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
_mtx[i][2] = v[2];
|
||||
}
|
||||
|
||||
/// Sets a column of the matrix from a Vec3.
|
||||
void SetColumn(int i, const GfVec3f & v) {
|
||||
_mtx[0][i] = v[0];
|
||||
_mtx[1][i] = v[1];
|
||||
_mtx[2][i] = v[2];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec3.
|
||||
GfVec3f GetRow(int i) const {
|
||||
return GfVec3f(_mtx[i][0], _mtx[i][1], _mtx[i][2]);
|
||||
}
|
||||
|
||||
/// Gets a column of the matrix as a Vec3.
|
||||
GfVec3f GetColumn(int i) const {
|
||||
return GfVec3f(_mtx[0][i], _mtx[1][i], _mtx[2][i]);
|
||||
}
|
||||
|
||||
/// Sets the matrix from 9 independent \c float values,
|
||||
/// specified in row-major order. For example, parameter \e m10 specifies
|
||||
/// the value in row 1 and column 0.
|
||||
GfMatrix3f& Set(float m00, float m01, float m02,
|
||||
float m10, float m11, float m12,
|
||||
float m20, float m21, float m22) {
|
||||
_mtx[0][0] = m00; _mtx[0][1] = m01; _mtx[0][2] = m02;
|
||||
_mtx[1][0] = m10; _mtx[1][1] = m11; _mtx[1][2] = m12;
|
||||
_mtx[2][0] = m20; _mtx[2][1] = m21; _mtx[2][2] = m22;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix from a 3x3 array of \c float
|
||||
/// values, specified in row-major order.
|
||||
GfMatrix3f& Set(const float m[3][3]) {
|
||||
_mtx[0][0] = m[0][0];
|
||||
_mtx[0][1] = m[0][1];
|
||||
_mtx[0][2] = m[0][2];
|
||||
_mtx[1][0] = m[1][0];
|
||||
_mtx[1][1] = m[1][1];
|
||||
_mtx[1][2] = m[1][2];
|
||||
_mtx[2][0] = m[2][0];
|
||||
_mtx[2][1] = m[2][1];
|
||||
_mtx[2][2] = m[2][2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix to the identity matrix.
|
||||
GfMatrix3f& SetIdentity() {
|
||||
return SetDiagonal(1);
|
||||
}
|
||||
|
||||
/// Sets the matrix to zero.
|
||||
GfMatrix3f& SetZero() {
|
||||
return SetDiagonal(0);
|
||||
}
|
||||
|
||||
/// Sets the matrix to \e s times the identity matrix.
|
||||
GF_API
|
||||
GfMatrix3f& SetDiagonal(float s);
|
||||
|
||||
/// Sets the matrix to have diagonal (<c>v[0], v[1], v[2]</c>).
|
||||
GF_API
|
||||
GfMatrix3f& SetDiagonal(const GfVec3f&);
|
||||
|
||||
/// Fills a 3x3 array of \c float values with the values in
|
||||
/// the matrix, specified in row-major order.
|
||||
GF_API
|
||||
float* Get(float m[3][3]) const;
|
||||
|
||||
/// Returns raw access to components of matrix as an array of
|
||||
/// \c float values. Components are in row-major order.
|
||||
float* data() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns const raw access to components of matrix as an array of
|
||||
/// \c float values. Components are in row-major order.
|
||||
const float* data() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as an array of \c float values.
|
||||
float* GetArray() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as a const array of \c float values.
|
||||
const float* GetArray() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 3 \c
|
||||
/// float values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
float* operator [](int i) { return _mtx[i]; }
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 3 \c
|
||||
/// float values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
const float* operator [](int i) const { return _mtx[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfMatrix3f const &m) {
|
||||
int nElems = 3 * 3;
|
||||
size_t h = 0;
|
||||
const float *p = m.GetArray();
|
||||
while (nElems--)
|
||||
boost::hash_combine(h, *p++);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix3d& m) const;
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix3f& m) const;
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix3d& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix3f& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Returns the transpose of the matrix.
|
||||
GF_API
|
||||
GfMatrix3f GetTranspose() const;
|
||||
|
||||
/// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
|
||||
/// matrix is singular. (FLT_MAX is the largest value a \c float can have,
|
||||
/// as defined by the system.) The matrix is considered singular if the
|
||||
/// determinant is less than or equal to the optional parameter \e eps. If
|
||||
/// \e det is non-null, <c>*det</c> is set to the determinant.
|
||||
GF_API
|
||||
GfMatrix3f GetInverse(double* det = NULL, double eps = 0) const;
|
||||
|
||||
/// Returns the determinant of the matrix.
|
||||
GF_API
|
||||
double GetDeterminant() const;
|
||||
|
||||
/// Makes the matrix orthonormal in place. This is an iterative method that
|
||||
/// is much more stable than the previous cross/cross method. If the
|
||||
/// iterative method does not converge, a warning is issued.
|
||||
///
|
||||
/// Returns true if the iteration converged, false otherwise. Leaves any
|
||||
/// translation part of the matrix unchanged. If \a issueWarning is true,
|
||||
/// this method will issue a warning if the iteration does not converge,
|
||||
/// otherwise it will be silent.
|
||||
GF_API
|
||||
bool Orthonormalize(bool issueWarning=true);
|
||||
|
||||
/// Returns an orthonormalized copy of the matrix.
|
||||
GF_API
|
||||
GfMatrix3f GetOrthonormalized(bool issueWarning=true) const;
|
||||
|
||||
/// Returns the sign of the determinant of the matrix, i.e. 1 for a
|
||||
/// right-handed matrix, -1 for a left-handed matrix, and 0 for a
|
||||
/// singular matrix.
|
||||
GF_API
|
||||
double GetHandedness() const;
|
||||
|
||||
/// Returns true if the vectors in the matrix form a right-handed
|
||||
/// coordinate system.
|
||||
bool IsRightHanded() const {
|
||||
return GetHandedness() == 1.0;
|
||||
}
|
||||
|
||||
/// Returns true if the vectors in matrix form a left-handed
|
||||
/// coordinate system.
|
||||
bool IsLeftHanded() const {
|
||||
return GetHandedness() == -1.0;
|
||||
}
|
||||
|
||||
/// Post-multiplies matrix \e m into this matrix.
|
||||
GF_API
|
||||
GfMatrix3f& operator *=(const GfMatrix3f& m);
|
||||
|
||||
/// Multiplies the matrix by a float.
|
||||
GF_API
|
||||
GfMatrix3f& operator *=(double);
|
||||
|
||||
/// Returns the product of a matrix and a float.
|
||||
friend GfMatrix3f operator *(const GfMatrix3f& m1, double d)
|
||||
{
|
||||
GfMatrix3f m = m1;
|
||||
return m *= d;
|
||||
}
|
||||
|
||||
///
|
||||
// Returns the product of a matrix and a float.
|
||||
friend GfMatrix3f operator *(double d, const GfMatrix3f& m)
|
||||
{
|
||||
return m * d;
|
||||
}
|
||||
|
||||
/// Adds matrix \e m to this matrix.
|
||||
GF_API
|
||||
GfMatrix3f& operator +=(const GfMatrix3f& m);
|
||||
|
||||
/// Subtracts matrix \e m from this matrix.
|
||||
GF_API
|
||||
GfMatrix3f& operator -=(const GfMatrix3f& m);
|
||||
|
||||
/// Returns the unary negation of matrix \e m.
|
||||
GF_API
|
||||
friend GfMatrix3f operator -(const GfMatrix3f& m);
|
||||
|
||||
/// Adds matrix \e m2 to \e m1
|
||||
friend GfMatrix3f operator +(const GfMatrix3f& m1, const GfMatrix3f& m2)
|
||||
{
|
||||
GfMatrix3f tmp(m1);
|
||||
tmp += m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtracts matrix \e m2 from \e m1.
|
||||
friend GfMatrix3f operator -(const GfMatrix3f& m1, const GfMatrix3f& m2)
|
||||
{
|
||||
GfMatrix3f tmp(m1);
|
||||
tmp -= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplies matrix \e m1 by \e m2.
|
||||
friend GfMatrix3f operator *(const GfMatrix3f& m1, const GfMatrix3f& m2)
|
||||
{
|
||||
GfMatrix3f tmp(m1);
|
||||
tmp *= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
|
||||
friend GfMatrix3f operator /(const GfMatrix3f& m1, const GfMatrix3f& m2)
|
||||
{
|
||||
return(m1 * m2.GetInverse());
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
friend inline GfVec3f operator *(const GfMatrix3f& m, const GfVec3f& vec) {
|
||||
return GfVec3f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1] + vec[2] * m._mtx[0][2],
|
||||
vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[1][2],
|
||||
vec[0] * m._mtx[2][0] + vec[1] * m._mtx[2][1] + vec[2] * m._mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
friend inline GfVec3f operator *(const GfVec3f &vec, const GfMatrix3f& m) {
|
||||
return GfVec3f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0] + vec[2] * m._mtx[2][0],
|
||||
vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[2][1],
|
||||
vec[0] * m._mtx[0][2] + vec[1] * m._mtx[1][2] + vec[2] * m._mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Sets matrix to specify a uniform scaling by \e scaleFactor.
|
||||
GF_API
|
||||
GfMatrix3f& SetScale(float scaleFactor);
|
||||
|
||||
/// \name 3D Transformation Utilities
|
||||
/// @{
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot.
|
||||
GF_API
|
||||
GfMatrix3f& SetRotate(const GfQuatf &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot.
|
||||
GF_API
|
||||
GfMatrix3f& SetRotate(const GfRotation &rot);
|
||||
|
||||
/// Sets the matrix to specify a nonuniform scaling in x, y, and z by
|
||||
/// the factors in vector \e scaleFactors.
|
||||
GF_API
|
||||
GfMatrix3f& SetScale(const GfVec3f &scaleFactors);
|
||||
|
||||
/// Returns the rotation corresponding to this matrix. This works
|
||||
/// well only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfRotation ExtractRotation() const;
|
||||
|
||||
/// Decompose the rotation corresponding to this matrix about 3
|
||||
/// orthogonal axes. If the axes are not orthogonal, warnings
|
||||
/// will be spewed.
|
||||
///
|
||||
/// This is a convenience method that is equivalent to calling
|
||||
/// ExtractRotation().Decompose().
|
||||
GF_API
|
||||
GfVec3f DecomposeRotation(const GfVec3f &axis0,
|
||||
const GfVec3f &axis1,
|
||||
const GfVec3f &axis2 ) const;
|
||||
|
||||
/// Returns the quaternion corresponding to this matrix. This works
|
||||
/// well only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfQuaternion ExtractRotationQuaternion() const;
|
||||
|
||||
/// @}
|
||||
|
||||
private:
|
||||
/// Set the matrix to the rotation given by a quaternion,
|
||||
/// defined by the real component \p r and imaginary components \p i.
|
||||
void _SetRotateFromQuat(float r, const GfVec3f& i);
|
||||
|
||||
|
||||
private:
|
||||
/// Matrix storage, in row-major order.
|
||||
GfMatrixData<float, 3, 3> _mtx;
|
||||
|
||||
// Friend declarations
|
||||
friend class GfMatrix3d;
|
||||
};
|
||||
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// difference between each component of the matrix is less than or equal
|
||||
/// to \p tolerance, or false otherwise.
|
||||
GF_API
|
||||
bool GfIsClose(GfMatrix3f const &m1, GfMatrix3f const &m2, double tolerance);
|
||||
|
||||
/// Output a GfMatrix3f
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfMatrix3f const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX3F_H
|
||||
@@ -0,0 +1,761 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// matrix4.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_MATRIX4D_H
|
||||
#define PXR_BASE_GF_MATRIX4D_H
|
||||
|
||||
/// \file gf/matrix4d.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/matrixData.h"
|
||||
#include "pxr/base/gf/vec4d.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/homogeneous.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfMatrix<class GfMatrix4d> { static const bool value = true; };
|
||||
|
||||
class GfMatrix4d;
|
||||
class GfMatrix4f;
|
||||
class GfQuatd;
|
||||
class GfRotation;
|
||||
class GfMatrix3d;
|
||||
|
||||
/// \class GfMatrix4d
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Stores a 4x4 matrix of \c double elements. A basic type.
|
||||
///
|
||||
/// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
|
||||
/// indexes the element in the \e i th row and the \e j th column.
|
||||
///
|
||||
/// <h3>3D Transformations</h3>
|
||||
///
|
||||
/// The following methods interpret a GfMatrix4d as a 3D
|
||||
/// transformation: SetRotate(), SetScale(), SetTranslate(), SetLookAt(),
|
||||
/// Factor(), ExtractTranslation(), ExtractRotation(), Transform(), TransformDir().
|
||||
/// By convention, vectors are treated primarily as row vectors,
|
||||
/// implying the following:
|
||||
/// \li Transformation matrices are organized to deal with row
|
||||
/// vectors, not column vectors. For example, the last row of a matrix
|
||||
/// contains the translation amounts.
|
||||
/// \li Each of the Set() methods below completely rewrites the
|
||||
/// matrix; for example, SetTranslate() yields a matrix
|
||||
/// which does nothing but translate.
|
||||
/// \li When multiplying two transformation matrices, the matrix
|
||||
/// on the left applies a more local transformation to a row
|
||||
/// vector. For example, if R represents a rotation
|
||||
/// matrix and T represents a translation matrix, the
|
||||
/// product R*T will rotate a row vector, then translate
|
||||
/// it.
|
||||
class GfMatrix4d
|
||||
{
|
||||
public:
|
||||
typedef double ScalarType;
|
||||
|
||||
static const size_t numRows = 4;
|
||||
static const size_t numColumns = 4;
|
||||
|
||||
/// Default constructor. Leaves the matrix component values undefined.
|
||||
GfMatrix4d() = default;
|
||||
|
||||
/// Constructor. Initializes the matrix from 16 independent
|
||||
/// \c double values, specified in row-major order. For example,
|
||||
/// parameter \e m10 specifies the value in row 1 and column 0.
|
||||
GfMatrix4d(double m00, double m01, double m02, double m03,
|
||||
double m10, double m11, double m12, double m13,
|
||||
double m20, double m21, double m22, double m23,
|
||||
double m30, double m31, double m32, double m33) {
|
||||
Set(m00, m01, m02, m03,
|
||||
m10, m11, m12, m13,
|
||||
m20, m21, m22, m23,
|
||||
m30, m31, m32, m33);
|
||||
}
|
||||
|
||||
/// Constructor. Initializes the matrix from a 4x4 array
|
||||
/// of \c double values, specified in row-major order.
|
||||
GfMatrix4d(const double m[4][4]) {
|
||||
Set(m);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to \e s times the
|
||||
/// identity matrix.
|
||||
explicit GfMatrix4d(double s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to diagonal form,
|
||||
/// with the \e i th element on the diagonal set to <c>v[i]</c>.
|
||||
explicit GfMatrix4d(const GfVec4d& v) {
|
||||
SetDiagonal(v);
|
||||
}
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// double. The vector is expected to be 4x4. If it is
|
||||
/// too big, only the first 4 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4d(const std::vector< std::vector<double> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// float. The vector is expected to be 4x4. If it is
|
||||
/// too big, only the first 4 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4d(const std::vector< std::vector<float> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from 4 row vectors of
|
||||
/// double. Each vector is expected to length 4. If it is too
|
||||
/// big, only the first 4 items will be used. If it is too small,
|
||||
/// uninitialized elements will be filled in with the
|
||||
/// corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4d(const std::vector<double>& r0,
|
||||
const std::vector<double>& r1,
|
||||
const std::vector<double>& r2,
|
||||
const std::vector<double>& r3);
|
||||
|
||||
/// Constructor. Initialize the matrix from 4 row vectors of
|
||||
/// float. Each vector is expected to length 4. If it is too
|
||||
/// big, only the first 4 items will be used. If it is too small,
|
||||
/// uninitialized elements will be filled in with the
|
||||
/// corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4d(const std::vector<float>& r0,
|
||||
const std::vector<float>& r1,
|
||||
const std::vector<float>& r2,
|
||||
const std::vector<float>& r3);
|
||||
|
||||
/// Constructor. Initializes a transformation matrix to perform the
|
||||
/// indicated rotation and translation.
|
||||
GF_API
|
||||
GfMatrix4d(const GfRotation& rotate,
|
||||
const GfVec3d& translate);
|
||||
|
||||
/// Constructor. Initializes a transformation matrix to perform the
|
||||
/// indicated rotation and translation.
|
||||
GF_API
|
||||
GfMatrix4d(const GfMatrix3d& rotmx,
|
||||
const GfVec3d& translate);
|
||||
/// This explicit constructor converts a "float" matrix to a "double" matrix.
|
||||
GF_API
|
||||
explicit GfMatrix4d(const class GfMatrix4f& m);
|
||||
|
||||
/// Sets a row of the matrix from a Vec4.
|
||||
void SetRow(int i, const GfVec4d & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
_mtx[i][2] = v[2];
|
||||
_mtx[i][3] = v[3];
|
||||
}
|
||||
|
||||
/// Sets a column of the matrix from a Vec4.
|
||||
void SetColumn(int i, const GfVec4d & v) {
|
||||
_mtx[0][i] = v[0];
|
||||
_mtx[1][i] = v[1];
|
||||
_mtx[2][i] = v[2];
|
||||
_mtx[3][i] = v[3];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec4.
|
||||
GfVec4d GetRow(int i) const {
|
||||
return GfVec4d(_mtx[i][0], _mtx[i][1], _mtx[i][2], _mtx[i][3]);
|
||||
}
|
||||
|
||||
/// Gets a column of the matrix as a Vec4.
|
||||
GfVec4d GetColumn(int i) const {
|
||||
return GfVec4d(_mtx[0][i], _mtx[1][i], _mtx[2][i], _mtx[3][i]);
|
||||
}
|
||||
|
||||
/// Sets the matrix from 16 independent \c double values,
|
||||
/// specified in row-major order. For example, parameter \e m10 specifies
|
||||
/// the value in row 1 and column 0.
|
||||
GfMatrix4d& Set(double m00, double m01, double m02, double m03,
|
||||
double m10, double m11, double m12, double m13,
|
||||
double m20, double m21, double m22, double m23,
|
||||
double m30, double m31, double m32, double m33) {
|
||||
_mtx[0][0] = m00; _mtx[0][1] = m01; _mtx[0][2] = m02; _mtx[0][3] = m03;
|
||||
_mtx[1][0] = m10; _mtx[1][1] = m11; _mtx[1][2] = m12; _mtx[1][3] = m13;
|
||||
_mtx[2][0] = m20; _mtx[2][1] = m21; _mtx[2][2] = m22; _mtx[2][3] = m23;
|
||||
_mtx[3][0] = m30; _mtx[3][1] = m31; _mtx[3][2] = m32; _mtx[3][3] = m33;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix from a 4x4 array of \c double
|
||||
/// values, specified in row-major order.
|
||||
GfMatrix4d& Set(const double m[4][4]) {
|
||||
_mtx[0][0] = m[0][0];
|
||||
_mtx[0][1] = m[0][1];
|
||||
_mtx[0][2] = m[0][2];
|
||||
_mtx[0][3] = m[0][3];
|
||||
_mtx[1][0] = m[1][0];
|
||||
_mtx[1][1] = m[1][1];
|
||||
_mtx[1][2] = m[1][2];
|
||||
_mtx[1][3] = m[1][3];
|
||||
_mtx[2][0] = m[2][0];
|
||||
_mtx[2][1] = m[2][1];
|
||||
_mtx[2][2] = m[2][2];
|
||||
_mtx[2][3] = m[2][3];
|
||||
_mtx[3][0] = m[3][0];
|
||||
_mtx[3][1] = m[3][1];
|
||||
_mtx[3][2] = m[3][2];
|
||||
_mtx[3][3] = m[3][3];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix to the identity matrix.
|
||||
GfMatrix4d& SetIdentity() {
|
||||
return SetDiagonal(1);
|
||||
}
|
||||
|
||||
/// Sets the matrix to zero.
|
||||
GfMatrix4d& SetZero() {
|
||||
return SetDiagonal(0);
|
||||
}
|
||||
|
||||
/// Sets the matrix to \e s times the identity matrix.
|
||||
GF_API
|
||||
GfMatrix4d& SetDiagonal(double s);
|
||||
|
||||
/// Sets the matrix to have diagonal (<c>v[0], v[1], v[2], v[3]</c>).
|
||||
GF_API
|
||||
GfMatrix4d& SetDiagonal(const GfVec4d&);
|
||||
|
||||
/// Fills a 4x4 array of \c double values with the values in
|
||||
/// the matrix, specified in row-major order.
|
||||
GF_API
|
||||
double* Get(double m[4][4]) const;
|
||||
|
||||
/// Returns raw access to components of matrix as an array of
|
||||
/// \c double values. Components are in row-major order.
|
||||
double* data() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns const raw access to components of matrix as an array of
|
||||
/// \c double values. Components are in row-major order.
|
||||
const double* data() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as an array of \c double values.
|
||||
double* GetArray() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as a const array of \c double values.
|
||||
const double* GetArray() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 4 \c
|
||||
/// double values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
double* operator [](int i) { return _mtx[i]; }
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 4 \c
|
||||
/// double values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
const double* operator [](int i) const { return _mtx[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfMatrix4d const &m) {
|
||||
int nElems = 4 * 4;
|
||||
size_t h = 0;
|
||||
const double *p = m.GetArray();
|
||||
while (nElems--)
|
||||
boost::hash_combine(h, *p++);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix4d& m) const;
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix4f& m) const;
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix4d& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix4f& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Returns the transpose of the matrix.
|
||||
GF_API
|
||||
GfMatrix4d GetTranspose() const;
|
||||
|
||||
/// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
|
||||
/// matrix is singular. (FLT_MAX is the largest value a \c float can have,
|
||||
/// as defined by the system.) The matrix is considered singular if the
|
||||
/// determinant is less than or equal to the optional parameter \e eps. If
|
||||
/// \e det is non-null, <c>*det</c> is set to the determinant.
|
||||
GF_API
|
||||
GfMatrix4d GetInverse(double* det = NULL, double eps = 0) const;
|
||||
|
||||
/// Returns the determinant of the matrix.
|
||||
GF_API
|
||||
double GetDeterminant() const;
|
||||
|
||||
/// Sets a row of the matrix from a Vec3.
|
||||
/// The fourth element of the row is ignored.
|
||||
void SetRow3(int i, const GfVec3d & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
_mtx[i][2] = v[2];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec3.
|
||||
GfVec3d GetRow3(int i) const {
|
||||
return GfVec3d(_mtx[i][0], _mtx[i][1], _mtx[i][2]);
|
||||
}
|
||||
|
||||
/// Returns the determinant of the upper 3x3 matrix. This method is useful
|
||||
/// when the matrix describes a linear transformation such as a rotation or
|
||||
/// scale because the other values in the 4x4 matrix are not important.
|
||||
double GetDeterminant3() const {
|
||||
return _GetDeterminant3(0, 1, 2, 0, 1, 2);
|
||||
}
|
||||
|
||||
/// Returns true, if the row vectors of the upper 3x3 matrix form an
|
||||
/// orthogonal basis. Note they do not have to be unit length for this
|
||||
/// test to return true.
|
||||
bool HasOrthogonalRows3() const {
|
||||
// XXX Should add GfAreOrthogonal(v0, v1, v2) (which also
|
||||
// GfRotation::Decompose() could use).
|
||||
GfVec3d axis0(GetRow3(0)), axis1(GetRow3(1)), axis2(GetRow3(2));
|
||||
return (GfAbs(GfDot(axis0, axis1)) < GF_MIN_ORTHO_TOLERANCE &&
|
||||
GfAbs(GfDot(axis0, axis2)) < GF_MIN_ORTHO_TOLERANCE &&
|
||||
GfAbs(GfDot(axis1, axis2)) < GF_MIN_ORTHO_TOLERANCE);
|
||||
}
|
||||
|
||||
/// Makes the matrix orthonormal in place. This is an iterative method
|
||||
/// that is much more stable than the previous cross/cross method. If the
|
||||
/// iterative method does not converge, a warning is issued.
|
||||
///
|
||||
/// Returns true if the iteration converged, false otherwise. Leaves any
|
||||
/// translation part of the matrix unchanged. If \a issueWarning is true,
|
||||
/// this method will issue a warning if the iteration does not converge,
|
||||
/// otherwise it will be silent.
|
||||
GF_API
|
||||
bool Orthonormalize(bool issueWarning=true);
|
||||
|
||||
/// Returns an orthonormalized copy of the matrix.
|
||||
GF_API
|
||||
GfMatrix4d GetOrthonormalized(bool issueWarning=true) const;
|
||||
|
||||
/// Returns the sign of the determinant of the upper 3x3 matrix, i.e. 1
|
||||
/// for a right-handed matrix, -1 for a left-handed matrix, and 0 for a
|
||||
/// singular matrix.
|
||||
GF_API
|
||||
double GetHandedness() const;
|
||||
|
||||
/// Returns true if the vectors in the upper 3x3 matrix form a
|
||||
/// right-handed coordinate system.
|
||||
bool IsRightHanded() const {
|
||||
return GetHandedness() == 1.0;
|
||||
}
|
||||
|
||||
/// Returns true if the vectors in the upper 3x3 matrix form a left-handed
|
||||
/// coordinate system.
|
||||
bool IsLeftHanded() const {
|
||||
return GetHandedness() == -1.0;
|
||||
}
|
||||
|
||||
/// Post-multiplies matrix \e m into this matrix.
|
||||
GF_API
|
||||
GfMatrix4d& operator *=(const GfMatrix4d& m);
|
||||
|
||||
/// Multiplies the matrix by a double.
|
||||
GF_API
|
||||
GfMatrix4d& operator *=(double);
|
||||
|
||||
/// Returns the product of a matrix and a double.
|
||||
friend GfMatrix4d operator *(const GfMatrix4d& m1, double d)
|
||||
{
|
||||
GfMatrix4d m = m1;
|
||||
return m *= d;
|
||||
}
|
||||
|
||||
///
|
||||
// Returns the product of a matrix and a double.
|
||||
friend GfMatrix4d operator *(double d, const GfMatrix4d& m)
|
||||
{
|
||||
return m * d;
|
||||
}
|
||||
|
||||
/// Adds matrix \e m to this matrix.
|
||||
GF_API
|
||||
GfMatrix4d& operator +=(const GfMatrix4d& m);
|
||||
|
||||
/// Subtracts matrix \e m from this matrix.
|
||||
GF_API
|
||||
GfMatrix4d& operator -=(const GfMatrix4d& m);
|
||||
|
||||
/// Returns the unary negation of matrix \e m.
|
||||
GF_API
|
||||
friend GfMatrix4d operator -(const GfMatrix4d& m);
|
||||
|
||||
/// Adds matrix \e m2 to \e m1
|
||||
friend GfMatrix4d operator +(const GfMatrix4d& m1, const GfMatrix4d& m2)
|
||||
{
|
||||
GfMatrix4d tmp(m1);
|
||||
tmp += m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtracts matrix \e m2 from \e m1.
|
||||
friend GfMatrix4d operator -(const GfMatrix4d& m1, const GfMatrix4d& m2)
|
||||
{
|
||||
GfMatrix4d tmp(m1);
|
||||
tmp -= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplies matrix \e m1 by \e m2.
|
||||
friend GfMatrix4d operator *(const GfMatrix4d& m1, const GfMatrix4d& m2)
|
||||
{
|
||||
GfMatrix4d tmp(m1);
|
||||
tmp *= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
|
||||
friend GfMatrix4d operator /(const GfMatrix4d& m1, const GfMatrix4d& m2)
|
||||
{
|
||||
return(m1 * m2.GetInverse());
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
friend inline GfVec4d operator *(const GfMatrix4d& m, const GfVec4d& vec) {
|
||||
return GfVec4d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1] + vec[2] * m._mtx[0][2] + vec[3] * m._mtx[0][3],
|
||||
vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[1][2] + vec[3] * m._mtx[1][3],
|
||||
vec[0] * m._mtx[2][0] + vec[1] * m._mtx[2][1] + vec[2] * m._mtx[2][2] + vec[3] * m._mtx[2][3],
|
||||
vec[0] * m._mtx[3][0] + vec[1] * m._mtx[3][1] + vec[2] * m._mtx[3][2] + vec[3] * m._mtx[3][3]);
|
||||
}
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
friend inline GfVec4d operator *(const GfVec4d &vec, const GfMatrix4d& m) {
|
||||
return GfVec4d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0] + vec[2] * m._mtx[2][0] + vec[3] * m._mtx[3][0],
|
||||
vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[2][1] + vec[3] * m._mtx[3][1],
|
||||
vec[0] * m._mtx[0][2] + vec[1] * m._mtx[1][2] + vec[2] * m._mtx[2][2] + vec[3] * m._mtx[3][2],
|
||||
vec[0] * m._mtx[0][3] + vec[1] * m._mtx[1][3] + vec[2] * m._mtx[2][3] + vec[3] * m._mtx[3][3]);
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
/// Note that the return type is a \c GfVec4f.
|
||||
GF_API
|
||||
friend GfVec4f operator *(const GfMatrix4d& m, const GfVec4f& vec);
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
/// Note that the return type is a \c GfVec4f.
|
||||
GF_API
|
||||
friend GfVec4f operator *(const GfVec4f &vec, const GfMatrix4d& m);
|
||||
|
||||
/// Sets matrix to specify a uniform scaling by \e scaleFactor.
|
||||
GF_API
|
||||
GfMatrix4d& SetScale(double scaleFactor);
|
||||
|
||||
/// Returns the matrix with any scaling or shearing removed,
|
||||
/// leaving only the rotation and translation.
|
||||
/// If the matrix cannot be decomposed, returns the original matrix.
|
||||
GF_API
|
||||
GfMatrix4d RemoveScaleShear() const;
|
||||
|
||||
/// \name 3D Transformation Utilities
|
||||
/// @{
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// and clears the translation.
|
||||
GF_API
|
||||
GfMatrix4d& SetRotate(const GfQuatd &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// without clearing the translation.
|
||||
GF_API
|
||||
GfMatrix4d& SetRotateOnly(const GfQuatd &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// and clears the translation.
|
||||
GF_API
|
||||
GfMatrix4d& SetRotate(const GfRotation &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// without clearing the translation.
|
||||
GF_API
|
||||
GfMatrix4d& SetRotateOnly(const GfRotation &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e mx,
|
||||
/// and clears the translation.
|
||||
GF_API
|
||||
GfMatrix4d& SetRotate(const GfMatrix3d &mx);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e mx,
|
||||
/// without clearing the translation.
|
||||
GF_API
|
||||
GfMatrix4d& SetRotateOnly(const GfMatrix3d &mx);
|
||||
|
||||
/// Sets the matrix to specify a nonuniform scaling in x, y, and z by
|
||||
/// the factors in vector \e scaleFactors.
|
||||
GF_API
|
||||
GfMatrix4d& SetScale(const GfVec3d &scaleFactors);
|
||||
|
||||
/// Sets matrix to specify a translation by the vector \e trans,
|
||||
/// and clears the rotation.
|
||||
GF_API
|
||||
GfMatrix4d& SetTranslate(const GfVec3d &trans);
|
||||
|
||||
/// Sets matrix to specify a translation by the vector \e trans,
|
||||
/// without clearing the rotation.
|
||||
GF_API
|
||||
GfMatrix4d& SetTranslateOnly(const GfVec3d &t);
|
||||
|
||||
/// Sets matrix to specify a rotation by \e rotate and a
|
||||
/// translation by \e translate.
|
||||
GF_API
|
||||
GfMatrix4d& SetTransform(const GfRotation& rotate,
|
||||
const GfVec3d& translate);
|
||||
|
||||
/// Sets matrix to specify a rotation by \e rotmx and a
|
||||
/// translation by \e translate.
|
||||
GF_API
|
||||
GfMatrix4d& SetTransform(const GfMatrix3d& rotmx,
|
||||
const GfVec3d& translate);
|
||||
|
||||
/// Sets the matrix to specify a viewing matrix from parameters
|
||||
/// similar to those used by <c>gluLookAt(3G)</c>. \e eyePoint
|
||||
/// represents the eye point in world space. \e centerPoint
|
||||
/// represents the world-space center of attention. \e upDirection
|
||||
/// is a vector indicating which way is up.
|
||||
GF_API
|
||||
GfMatrix4d& SetLookAt(const GfVec3d &eyePoint,
|
||||
const GfVec3d ¢erPoint,
|
||||
const GfVec3d &upDirection);
|
||||
|
||||
/// Sets the matrix to specify a viewing matrix from a world-space
|
||||
/// \e eyePoint and a world-space rotation that rigidly rotates the
|
||||
/// orientation from its canonical frame, which is defined to be
|
||||
/// looking along the <c>-z</c> axis with the <c>+y</c> axis as the up
|
||||
/// direction.
|
||||
GF_API
|
||||
GfMatrix4d& SetLookAt(const GfVec3d &eyePoint,
|
||||
const GfRotation &orientation);
|
||||
|
||||
/// Factors the matrix into 5 components:
|
||||
/// \li <c>\e M = r * s * -r * u * t</c>
|
||||
/// where
|
||||
/// \li \e t is a translation.
|
||||
/// \li \e u and \e r are rotations, and \e -r is the transpose
|
||||
/// (inverse) of \e r. The \e u matrix may contain shear
|
||||
/// information.
|
||||
/// \li \e s is a scale.
|
||||
/// Any projection information could be returned in matrix \e p,
|
||||
/// but currently p is never modified.
|
||||
///
|
||||
/// Returns \c false if the matrix is singular (as determined by \e eps).
|
||||
/// In that case, any zero scales in \e s are clamped to \e eps
|
||||
/// to allow computation of \e u.
|
||||
GF_API
|
||||
bool Factor(GfMatrix4d* r, GfVec3d* s, GfMatrix4d* u,
|
||||
GfVec3d* t, GfMatrix4d* p,
|
||||
double eps = 1e-10) const;
|
||||
|
||||
/// Returns the translation part of the matrix, defined as the first three
|
||||
/// elements of the last row.
|
||||
GfVec3d ExtractTranslation() const {
|
||||
return GfVec3d(_mtx[3][0], _mtx[3][1], _mtx[3][2]);
|
||||
}
|
||||
|
||||
/// Returns the rotation corresponding to this matrix. This works well
|
||||
/// only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfRotation ExtractRotation() const;
|
||||
|
||||
/// Return the rotation corresponding to this matrix as a quaternion.
|
||||
/// This works well only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfQuatd ExtractRotationQuat() const;
|
||||
|
||||
/// Decompose the rotation corresponding to this matrix about 3 orthogonal
|
||||
/// axes. If the axes are not orthogonal, warnings will be spewed.
|
||||
///
|
||||
/// This is a convenience method that is equivalent to calling
|
||||
/// ExtractRotation().Decompose().
|
||||
GF_API
|
||||
GfVec3d DecomposeRotation(const GfVec3d &axis0,
|
||||
const GfVec3d &axis1,
|
||||
const GfVec3d &axis2) const;
|
||||
|
||||
/// Returns the rotation corresponding to this matrix. This works well
|
||||
/// only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfMatrix3d ExtractRotationMatrix() const;
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1.
|
||||
GfVec3d Transform(const GfVec3d &vec) const {
|
||||
return GfProject(GfVec4d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2],
|
||||
vec[0] * _mtx[0][3] + vec[1] * _mtx[1][3] + vec[2] * _mtx[2][3] + _mtx[3][3]));
|
||||
}
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1. This is an overloaded method; it differs from the other version
|
||||
/// in that it returns a different value type.
|
||||
GfVec3f Transform(const GfVec3f &vec) const {
|
||||
return GfVec3f(GfProject(GfVec4d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2],
|
||||
vec[0] * _mtx[0][3] + vec[1] * _mtx[1][3] + vec[2] * _mtx[2][3] + _mtx[3][3])));
|
||||
}
|
||||
|
||||
/// Transforms row vector \e vec by the matrix, returning the result. This
|
||||
/// treats the vector as a direction vector, so the translation
|
||||
/// information in the matrix is ignored. That is, it treats the vector as
|
||||
/// a 4-component vector whose fourth component is 0.
|
||||
GfVec3d TransformDir(const GfVec3d &vec) const {
|
||||
return GfVec3d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Transforms row vector \e vec by the matrix, returning the result. This
|
||||
/// treats the vector as a direction vector, so the translation
|
||||
/// information in the matrix is ignored. That is, it treats the vector as
|
||||
/// a 4-component vector whose fourth component is 0. This is an
|
||||
/// overloaded method; it differs from the other version in that it
|
||||
/// returns a different value type.
|
||||
GfVec3f TransformDir(const GfVec3f &vec) const {
|
||||
return GfVec3f(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1 and ignores the fourth column of the matrix (i.e. assumes it is
|
||||
/// (0, 0, 0, 1)).
|
||||
GfVec3d TransformAffine(const GfVec3d &vec) const {
|
||||
return GfVec3d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2]);
|
||||
}
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1 and ignores the fourth column of the matrix (i.e. assumes it is
|
||||
/// (0, 0, 0, 1)).
|
||||
GfVec3f TransformAffine(const GfVec3f &vec) const {
|
||||
return GfVec3f(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2]);
|
||||
}
|
||||
/// @}
|
||||
|
||||
private:
|
||||
/// Returns the determinant of the 3x3 submatrix specified by the three
|
||||
/// given row and column indices (0-3 for each).
|
||||
GF_API
|
||||
double _GetDeterminant3(size_t row1, size_t row2, size_t row3,
|
||||
size_t col1, size_t col2, size_t col3) const;
|
||||
|
||||
/// Diagonalizes the upper 3x3 matrix of a matrix known to be symmetric.
|
||||
void _Jacobi3(GfVec3d *eigenvalues, GfVec3d eigenvectors[3]) const;
|
||||
|
||||
/// Set the 3x3 submatrix to the rotation given by a quaternion,
|
||||
/// defined by the real component \p r and imaginary components \p i.
|
||||
void _SetRotateFromQuat(double r, const GfVec3d& i);
|
||||
|
||||
|
||||
private:
|
||||
/// Matrix storage, in row-major order.
|
||||
GfMatrixData<double, 4, 4> _mtx;
|
||||
|
||||
// Friend declarations
|
||||
friend class GfMatrix4f;
|
||||
};
|
||||
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// difference between each component of the matrix is less than or equal
|
||||
/// to \p tolerance, or false otherwise.
|
||||
GF_API
|
||||
bool GfIsClose(GfMatrix4d const &m1, GfMatrix4d const &m2, double tolerance);
|
||||
|
||||
/// Output a GfMatrix4d
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfMatrix4d const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX4D_H
|
||||
@@ -0,0 +1,751 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// matrix4.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_MATRIX4F_H
|
||||
#define PXR_BASE_GF_MATRIX4F_H
|
||||
|
||||
/// \file gf/matrix4f.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/matrixData.h"
|
||||
#include "pxr/base/gf/vec4f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/homogeneous.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfMatrix<class GfMatrix4f> { static const bool value = true; };
|
||||
|
||||
class GfMatrix4d;
|
||||
class GfMatrix4f;
|
||||
class GfQuatf;
|
||||
class GfRotation;
|
||||
class GfMatrix3f;
|
||||
|
||||
/// \class GfMatrix4f
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Stores a 4x4 matrix of \c float elements. A basic type.
|
||||
///
|
||||
/// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
|
||||
/// indexes the element in the \e i th row and the \e j th column.
|
||||
///
|
||||
/// <h3>3D Transformations</h3>
|
||||
///
|
||||
/// The following methods interpret a GfMatrix4f as a 3D
|
||||
/// transformation: SetRotate(), SetScale(), SetTranslate(), SetLookAt(),
|
||||
/// Factor(), ExtractTranslation(), ExtractRotation(), Transform(), TransformDir().
|
||||
/// By convention, vectors are treated primarily as row vectors,
|
||||
/// implying the following:
|
||||
/// \li Transformation matrices are organized to deal with row
|
||||
/// vectors, not column vectors. For example, the last row of a matrix
|
||||
/// contains the translation amounts.
|
||||
/// \li Each of the Set() methods below completely rewrites the
|
||||
/// matrix; for example, SetTranslate() yields a matrix
|
||||
/// which does nothing but translate.
|
||||
/// \li When multiplying two transformation matrices, the matrix
|
||||
/// on the left applies a more local transformation to a row
|
||||
/// vector. For example, if R represents a rotation
|
||||
/// matrix and T represents a translation matrix, the
|
||||
/// product R*T will rotate a row vector, then translate
|
||||
/// it.
|
||||
class GfMatrix4f
|
||||
{
|
||||
public:
|
||||
typedef float ScalarType;
|
||||
|
||||
static const size_t numRows = 4;
|
||||
static const size_t numColumns = 4;
|
||||
|
||||
/// Default constructor. Leaves the matrix component values undefined.
|
||||
GfMatrix4f() = default;
|
||||
|
||||
/// Constructor. Initializes the matrix from 16 independent
|
||||
/// \c float values, specified in row-major order. For example,
|
||||
/// parameter \e m10 specifies the value in row 1 and column 0.
|
||||
GfMatrix4f(float m00, float m01, float m02, float m03,
|
||||
float m10, float m11, float m12, float m13,
|
||||
float m20, float m21, float m22, float m23,
|
||||
float m30, float m31, float m32, float m33) {
|
||||
Set(m00, m01, m02, m03,
|
||||
m10, m11, m12, m13,
|
||||
m20, m21, m22, m23,
|
||||
m30, m31, m32, m33);
|
||||
}
|
||||
|
||||
/// Constructor. Initializes the matrix from a 4x4 array
|
||||
/// of \c float values, specified in row-major order.
|
||||
GfMatrix4f(const float m[4][4]) {
|
||||
Set(m);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to \e s times the
|
||||
/// identity matrix.
|
||||
explicit GfMatrix4f(float s) {
|
||||
SetDiagonal(s);
|
||||
}
|
||||
|
||||
/// Constructor. Explicitly initializes the matrix to diagonal form,
|
||||
/// with the \e i th element on the diagonal set to <c>v[i]</c>.
|
||||
explicit GfMatrix4f(const GfVec4f& v) {
|
||||
SetDiagonal(v);
|
||||
}
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// double. The vector is expected to be 4x4. If it is
|
||||
/// too big, only the first 4 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4f(const std::vector< std::vector<double> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from a vector of vectors of
|
||||
/// float. The vector is expected to be 4x4. If it is
|
||||
/// too big, only the first 4 rows and/or columns will be used.
|
||||
/// If it is too small, uninitialized elements will be filled in with
|
||||
/// the corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4f(const std::vector< std::vector<float> >& v);
|
||||
|
||||
/// Constructor. Initialize the matrix from 4 row vectors of
|
||||
/// double. Each vector is expected to length 4. If it is too
|
||||
/// big, only the first 4 items will be used. If it is too small,
|
||||
/// uninitialized elements will be filled in with the
|
||||
/// corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4f(const std::vector<double>& r0,
|
||||
const std::vector<double>& r1,
|
||||
const std::vector<double>& r2,
|
||||
const std::vector<double>& r3);
|
||||
|
||||
/// Constructor. Initialize the matrix from 4 row vectors of
|
||||
/// float. Each vector is expected to length 4. If it is too
|
||||
/// big, only the first 4 items will be used. If it is too small,
|
||||
/// uninitialized elements will be filled in with the
|
||||
/// corresponding elements from an identity matrix.
|
||||
///
|
||||
GF_API
|
||||
explicit GfMatrix4f(const std::vector<float>& r0,
|
||||
const std::vector<float>& r1,
|
||||
const std::vector<float>& r2,
|
||||
const std::vector<float>& r3);
|
||||
|
||||
/// Constructor. Initializes a transformation matrix to perform the
|
||||
/// indicated rotation and translation.
|
||||
GF_API
|
||||
GfMatrix4f(const GfRotation& rotate,
|
||||
const GfVec3f& translate);
|
||||
|
||||
/// Constructor. Initializes a transformation matrix to perform the
|
||||
/// indicated rotation and translation.
|
||||
GF_API
|
||||
GfMatrix4f(const GfMatrix3f& rotmx,
|
||||
const GfVec3f& translate);
|
||||
/// This explicit constructor converts a "double" matrix to a "float" matrix.
|
||||
GF_API
|
||||
explicit GfMatrix4f(const class GfMatrix4d& m);
|
||||
|
||||
/// Sets a row of the matrix from a Vec4.
|
||||
void SetRow(int i, const GfVec4f & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
_mtx[i][2] = v[2];
|
||||
_mtx[i][3] = v[3];
|
||||
}
|
||||
|
||||
/// Sets a column of the matrix from a Vec4.
|
||||
void SetColumn(int i, const GfVec4f & v) {
|
||||
_mtx[0][i] = v[0];
|
||||
_mtx[1][i] = v[1];
|
||||
_mtx[2][i] = v[2];
|
||||
_mtx[3][i] = v[3];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec4.
|
||||
GfVec4f GetRow(int i) const {
|
||||
return GfVec4f(_mtx[i][0], _mtx[i][1], _mtx[i][2], _mtx[i][3]);
|
||||
}
|
||||
|
||||
/// Gets a column of the matrix as a Vec4.
|
||||
GfVec4f GetColumn(int i) const {
|
||||
return GfVec4f(_mtx[0][i], _mtx[1][i], _mtx[2][i], _mtx[3][i]);
|
||||
}
|
||||
|
||||
/// Sets the matrix from 16 independent \c float values,
|
||||
/// specified in row-major order. For example, parameter \e m10 specifies
|
||||
/// the value in row 1 and column 0.
|
||||
GfMatrix4f& Set(float m00, float m01, float m02, float m03,
|
||||
float m10, float m11, float m12, float m13,
|
||||
float m20, float m21, float m22, float m23,
|
||||
float m30, float m31, float m32, float m33) {
|
||||
_mtx[0][0] = m00; _mtx[0][1] = m01; _mtx[0][2] = m02; _mtx[0][3] = m03;
|
||||
_mtx[1][0] = m10; _mtx[1][1] = m11; _mtx[1][2] = m12; _mtx[1][3] = m13;
|
||||
_mtx[2][0] = m20; _mtx[2][1] = m21; _mtx[2][2] = m22; _mtx[2][3] = m23;
|
||||
_mtx[3][0] = m30; _mtx[3][1] = m31; _mtx[3][2] = m32; _mtx[3][3] = m33;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix from a 4x4 array of \c float
|
||||
/// values, specified in row-major order.
|
||||
GfMatrix4f& Set(const float m[4][4]) {
|
||||
_mtx[0][0] = m[0][0];
|
||||
_mtx[0][1] = m[0][1];
|
||||
_mtx[0][2] = m[0][2];
|
||||
_mtx[0][3] = m[0][3];
|
||||
_mtx[1][0] = m[1][0];
|
||||
_mtx[1][1] = m[1][1];
|
||||
_mtx[1][2] = m[1][2];
|
||||
_mtx[1][3] = m[1][3];
|
||||
_mtx[2][0] = m[2][0];
|
||||
_mtx[2][1] = m[2][1];
|
||||
_mtx[2][2] = m[2][2];
|
||||
_mtx[2][3] = m[2][3];
|
||||
_mtx[3][0] = m[3][0];
|
||||
_mtx[3][1] = m[3][1];
|
||||
_mtx[3][2] = m[3][2];
|
||||
_mtx[3][3] = m[3][3];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the matrix to the identity matrix.
|
||||
GfMatrix4f& SetIdentity() {
|
||||
return SetDiagonal(1);
|
||||
}
|
||||
|
||||
/// Sets the matrix to zero.
|
||||
GfMatrix4f& SetZero() {
|
||||
return SetDiagonal(0);
|
||||
}
|
||||
|
||||
/// Sets the matrix to \e s times the identity matrix.
|
||||
GF_API
|
||||
GfMatrix4f& SetDiagonal(float s);
|
||||
|
||||
/// Sets the matrix to have diagonal (<c>v[0], v[1], v[2], v[3]</c>).
|
||||
GF_API
|
||||
GfMatrix4f& SetDiagonal(const GfVec4f&);
|
||||
|
||||
/// Fills a 4x4 array of \c float values with the values in
|
||||
/// the matrix, specified in row-major order.
|
||||
GF_API
|
||||
float* Get(float m[4][4]) const;
|
||||
|
||||
/// Returns raw access to components of matrix as an array of
|
||||
/// \c float values. Components are in row-major order.
|
||||
float* data() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns const raw access to components of matrix as an array of
|
||||
/// \c float values. Components are in row-major order.
|
||||
const float* data() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as an array of \c float values.
|
||||
float* GetArray() {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Returns vector components as a const array of \c float values.
|
||||
const float* GetArray() const {
|
||||
return _mtx.GetData();
|
||||
}
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 4 \c
|
||||
/// float values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
float* operator [](int i) { return _mtx[i]; }
|
||||
|
||||
/// Accesses an indexed row \e i of the matrix as an array of 4 \c
|
||||
/// float values so that standard indexing (such as <c>m[0][1]</c>)
|
||||
/// works correctly.
|
||||
const float* operator [](int i) const { return _mtx[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfMatrix4f const &m) {
|
||||
int nElems = 4 * 4;
|
||||
size_t h = 0;
|
||||
const float *p = m.GetArray();
|
||||
while (nElems--)
|
||||
boost::hash_combine(h, *p++);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix4d& m) const;
|
||||
|
||||
/// Tests for element-wise matrix equality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfMatrix4f& m) const;
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix4d& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Tests for element-wise matrix inequality. All elements must match
|
||||
/// exactly for matrices to be considered equal.
|
||||
bool operator !=(const GfMatrix4f& m) const {
|
||||
return !(*this == m);
|
||||
}
|
||||
|
||||
/// Returns the transpose of the matrix.
|
||||
GF_API
|
||||
GfMatrix4f GetTranspose() const;
|
||||
|
||||
/// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
|
||||
/// matrix is singular. (FLT_MAX is the largest value a \c float can have,
|
||||
/// as defined by the system.) The matrix is considered singular if the
|
||||
/// determinant is less than or equal to the optional parameter \e eps. If
|
||||
/// \e det is non-null, <c>*det</c> is set to the determinant.
|
||||
GF_API
|
||||
GfMatrix4f GetInverse(double* det = NULL, double eps = 0) const;
|
||||
|
||||
/// Returns the determinant of the matrix.
|
||||
GF_API
|
||||
double GetDeterminant() const;
|
||||
|
||||
/// Sets a row of the matrix from a Vec3.
|
||||
/// The fourth element of the row is ignored.
|
||||
void SetRow3(int i, const GfVec3f & v) {
|
||||
_mtx[i][0] = v[0];
|
||||
_mtx[i][1] = v[1];
|
||||
_mtx[i][2] = v[2];
|
||||
}
|
||||
|
||||
/// Gets a row of the matrix as a Vec3.
|
||||
GfVec3f GetRow3(int i) const {
|
||||
return GfVec3f(_mtx[i][0], _mtx[i][1], _mtx[i][2]);
|
||||
}
|
||||
|
||||
/// Returns the determinant of the upper 3x3 matrix. This method is useful
|
||||
/// when the matrix describes a linear transformation such as a rotation or
|
||||
/// scale because the other values in the 4x4 matrix are not important.
|
||||
double GetDeterminant3() const {
|
||||
return _GetDeterminant3(0, 1, 2, 0, 1, 2);
|
||||
}
|
||||
|
||||
/// Returns true, if the row vectors of the upper 3x3 matrix form an
|
||||
/// orthogonal basis. Note they do not have to be unit length for this
|
||||
/// test to return true.
|
||||
bool HasOrthogonalRows3() const {
|
||||
// XXX Should add GfAreOrthogonal(v0, v1, v2) (which also
|
||||
// GfRotation::Decompose() could use).
|
||||
GfVec3f axis0(GetRow3(0)), axis1(GetRow3(1)), axis2(GetRow3(2));
|
||||
return (GfAbs(GfDot(axis0, axis1)) < GF_MIN_ORTHO_TOLERANCE &&
|
||||
GfAbs(GfDot(axis0, axis2)) < GF_MIN_ORTHO_TOLERANCE &&
|
||||
GfAbs(GfDot(axis1, axis2)) < GF_MIN_ORTHO_TOLERANCE);
|
||||
}
|
||||
|
||||
/// Makes the matrix orthonormal in place. This is an iterative method
|
||||
/// that is much more stable than the previous cross/cross method. If the
|
||||
/// iterative method does not converge, a warning is issued.
|
||||
///
|
||||
/// Returns true if the iteration converged, false otherwise. Leaves any
|
||||
/// translation part of the matrix unchanged. If \a issueWarning is true,
|
||||
/// this method will issue a warning if the iteration does not converge,
|
||||
/// otherwise it will be silent.
|
||||
GF_API
|
||||
bool Orthonormalize(bool issueWarning=true);
|
||||
|
||||
/// Returns an orthonormalized copy of the matrix.
|
||||
GF_API
|
||||
GfMatrix4f GetOrthonormalized(bool issueWarning=true) const;
|
||||
|
||||
/// Returns the sign of the determinant of the upper 3x3 matrix, i.e. 1
|
||||
/// for a right-handed matrix, -1 for a left-handed matrix, and 0 for a
|
||||
/// singular matrix.
|
||||
GF_API
|
||||
double GetHandedness() const;
|
||||
|
||||
/// Returns true if the vectors in the upper 3x3 matrix form a
|
||||
/// right-handed coordinate system.
|
||||
bool IsRightHanded() const {
|
||||
return GetHandedness() == 1.0;
|
||||
}
|
||||
|
||||
/// Returns true if the vectors in the upper 3x3 matrix form a left-handed
|
||||
/// coordinate system.
|
||||
bool IsLeftHanded() const {
|
||||
return GetHandedness() == -1.0;
|
||||
}
|
||||
|
||||
/// Post-multiplies matrix \e m into this matrix.
|
||||
GF_API
|
||||
GfMatrix4f& operator *=(const GfMatrix4f& m);
|
||||
|
||||
/// Multiplies the matrix by a float.
|
||||
GF_API
|
||||
GfMatrix4f& operator *=(double);
|
||||
|
||||
/// Returns the product of a matrix and a float.
|
||||
friend GfMatrix4f operator *(const GfMatrix4f& m1, double d)
|
||||
{
|
||||
GfMatrix4f m = m1;
|
||||
return m *= d;
|
||||
}
|
||||
|
||||
///
|
||||
// Returns the product of a matrix and a float.
|
||||
friend GfMatrix4f operator *(double d, const GfMatrix4f& m)
|
||||
{
|
||||
return m * d;
|
||||
}
|
||||
|
||||
/// Adds matrix \e m to this matrix.
|
||||
GF_API
|
||||
GfMatrix4f& operator +=(const GfMatrix4f& m);
|
||||
|
||||
/// Subtracts matrix \e m from this matrix.
|
||||
GF_API
|
||||
GfMatrix4f& operator -=(const GfMatrix4f& m);
|
||||
|
||||
/// Returns the unary negation of matrix \e m.
|
||||
GF_API
|
||||
friend GfMatrix4f operator -(const GfMatrix4f& m);
|
||||
|
||||
/// Adds matrix \e m2 to \e m1
|
||||
friend GfMatrix4f operator +(const GfMatrix4f& m1, const GfMatrix4f& m2)
|
||||
{
|
||||
GfMatrix4f tmp(m1);
|
||||
tmp += m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Subtracts matrix \e m2 from \e m1.
|
||||
friend GfMatrix4f operator -(const GfMatrix4f& m1, const GfMatrix4f& m2)
|
||||
{
|
||||
GfMatrix4f tmp(m1);
|
||||
tmp -= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Multiplies matrix \e m1 by \e m2.
|
||||
friend GfMatrix4f operator *(const GfMatrix4f& m1, const GfMatrix4f& m2)
|
||||
{
|
||||
GfMatrix4f tmp(m1);
|
||||
tmp *= m2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
|
||||
friend GfMatrix4f operator /(const GfMatrix4f& m1, const GfMatrix4f& m2)
|
||||
{
|
||||
return(m1 * m2.GetInverse());
|
||||
}
|
||||
|
||||
/// Returns the product of a matrix \e m and a column vector \e vec.
|
||||
friend inline GfVec4f operator *(const GfMatrix4f& m, const GfVec4f& vec) {
|
||||
return GfVec4f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1] + vec[2] * m._mtx[0][2] + vec[3] * m._mtx[0][3],
|
||||
vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[1][2] + vec[3] * m._mtx[1][3],
|
||||
vec[0] * m._mtx[2][0] + vec[1] * m._mtx[2][1] + vec[2] * m._mtx[2][2] + vec[3] * m._mtx[2][3],
|
||||
vec[0] * m._mtx[3][0] + vec[1] * m._mtx[3][1] + vec[2] * m._mtx[3][2] + vec[3] * m._mtx[3][3]);
|
||||
}
|
||||
|
||||
/// Returns the product of row vector \e vec and a matrix \e m.
|
||||
friend inline GfVec4f operator *(const GfVec4f &vec, const GfMatrix4f& m) {
|
||||
return GfVec4f(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0] + vec[2] * m._mtx[2][0] + vec[3] * m._mtx[3][0],
|
||||
vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[2][1] + vec[3] * m._mtx[3][1],
|
||||
vec[0] * m._mtx[0][2] + vec[1] * m._mtx[1][2] + vec[2] * m._mtx[2][2] + vec[3] * m._mtx[3][2],
|
||||
vec[0] * m._mtx[0][3] + vec[1] * m._mtx[1][3] + vec[2] * m._mtx[2][3] + vec[3] * m._mtx[3][3]);
|
||||
}
|
||||
|
||||
/// Sets matrix to specify a uniform scaling by \e scaleFactor.
|
||||
GF_API
|
||||
GfMatrix4f& SetScale(float scaleFactor);
|
||||
|
||||
/// Returns the matrix with any scaling or shearing removed,
|
||||
/// leaving only the rotation and translation.
|
||||
/// If the matrix cannot be decomposed, returns the original matrix.
|
||||
GF_API
|
||||
GfMatrix4f RemoveScaleShear() const;
|
||||
|
||||
/// \name 3D Transformation Utilities
|
||||
/// @{
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// and clears the translation.
|
||||
GF_API
|
||||
GfMatrix4f& SetRotate(const GfQuatf &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// without clearing the translation.
|
||||
GF_API
|
||||
GfMatrix4f& SetRotateOnly(const GfQuatf &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// and clears the translation.
|
||||
GF_API
|
||||
GfMatrix4f& SetRotate(const GfRotation &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e rot,
|
||||
/// without clearing the translation.
|
||||
GF_API
|
||||
GfMatrix4f& SetRotateOnly(const GfRotation &rot);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e mx,
|
||||
/// and clears the translation.
|
||||
GF_API
|
||||
GfMatrix4f& SetRotate(const GfMatrix3f &mx);
|
||||
|
||||
/// Sets the matrix to specify a rotation equivalent to \e mx,
|
||||
/// without clearing the translation.
|
||||
GF_API
|
||||
GfMatrix4f& SetRotateOnly(const GfMatrix3f &mx);
|
||||
|
||||
/// Sets the matrix to specify a nonuniform scaling in x, y, and z by
|
||||
/// the factors in vector \e scaleFactors.
|
||||
GF_API
|
||||
GfMatrix4f& SetScale(const GfVec3f &scaleFactors);
|
||||
|
||||
/// Sets matrix to specify a translation by the vector \e trans,
|
||||
/// and clears the rotation.
|
||||
GF_API
|
||||
GfMatrix4f& SetTranslate(const GfVec3f &trans);
|
||||
|
||||
/// Sets matrix to specify a translation by the vector \e trans,
|
||||
/// without clearing the rotation.
|
||||
GF_API
|
||||
GfMatrix4f& SetTranslateOnly(const GfVec3f &t);
|
||||
|
||||
/// Sets matrix to specify a rotation by \e rotate and a
|
||||
/// translation by \e translate.
|
||||
GF_API
|
||||
GfMatrix4f& SetTransform(const GfRotation& rotate,
|
||||
const GfVec3f& translate);
|
||||
|
||||
/// Sets matrix to specify a rotation by \e rotmx and a
|
||||
/// translation by \e translate.
|
||||
GF_API
|
||||
GfMatrix4f& SetTransform(const GfMatrix3f& rotmx,
|
||||
const GfVec3f& translate);
|
||||
|
||||
/// Sets the matrix to specify a viewing matrix from parameters
|
||||
/// similar to those used by <c>gluLookAt(3G)</c>. \e eyePoint
|
||||
/// represents the eye point in world space. \e centerPoint
|
||||
/// represents the world-space center of attention. \e upDirection
|
||||
/// is a vector indicating which way is up.
|
||||
GF_API
|
||||
GfMatrix4f& SetLookAt(const GfVec3f &eyePoint,
|
||||
const GfVec3f ¢erPoint,
|
||||
const GfVec3f &upDirection);
|
||||
|
||||
/// Sets the matrix to specify a viewing matrix from a world-space
|
||||
/// \e eyePoint and a world-space rotation that rigidly rotates the
|
||||
/// orientation from its canonical frame, which is defined to be
|
||||
/// looking along the <c>-z</c> axis with the <c>+y</c> axis as the up
|
||||
/// direction.
|
||||
GF_API
|
||||
GfMatrix4f& SetLookAt(const GfVec3f &eyePoint,
|
||||
const GfRotation &orientation);
|
||||
|
||||
/// Factors the matrix into 5 components:
|
||||
/// \li <c>\e M = r * s * -r * u * t</c>
|
||||
/// where
|
||||
/// \li \e t is a translation.
|
||||
/// \li \e u and \e r are rotations, and \e -r is the transpose
|
||||
/// (inverse) of \e r. The \e u matrix may contain shear
|
||||
/// information.
|
||||
/// \li \e s is a scale.
|
||||
/// Any projection information could be returned in matrix \e p,
|
||||
/// but currently p is never modified.
|
||||
///
|
||||
/// Returns \c false if the matrix is singular (as determined by \e eps).
|
||||
/// In that case, any zero scales in \e s are clamped to \e eps
|
||||
/// to allow computation of \e u.
|
||||
GF_API
|
||||
bool Factor(GfMatrix4f* r, GfVec3f* s, GfMatrix4f* u,
|
||||
GfVec3f* t, GfMatrix4f* p,
|
||||
float eps = 1e-5) const;
|
||||
|
||||
/// Returns the translation part of the matrix, defined as the first three
|
||||
/// elements of the last row.
|
||||
GfVec3f ExtractTranslation() const {
|
||||
return GfVec3f(_mtx[3][0], _mtx[3][1], _mtx[3][2]);
|
||||
}
|
||||
|
||||
/// Returns the rotation corresponding to this matrix. This works well
|
||||
/// only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfRotation ExtractRotation() const;
|
||||
|
||||
/// Return the rotation corresponding to this matrix as a quaternion.
|
||||
/// This works well only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfQuatf ExtractRotationQuat() const;
|
||||
|
||||
/// Decompose the rotation corresponding to this matrix about 3 orthogonal
|
||||
/// axes. If the axes are not orthogonal, warnings will be spewed.
|
||||
///
|
||||
/// This is a convenience method that is equivalent to calling
|
||||
/// ExtractRotation().Decompose().
|
||||
GF_API
|
||||
GfVec3f DecomposeRotation(const GfVec3f &axis0,
|
||||
const GfVec3f &axis1,
|
||||
const GfVec3f &axis2) const;
|
||||
|
||||
/// Returns the rotation corresponding to this matrix. This works well
|
||||
/// only if the matrix represents a rotation.
|
||||
///
|
||||
/// For good results, consider calling Orthonormalize() before calling
|
||||
/// this method.
|
||||
GF_API
|
||||
GfMatrix3f ExtractRotationMatrix() const;
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1.
|
||||
GfVec3d Transform(const GfVec3d &vec) const {
|
||||
return GfProject(GfVec4d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2],
|
||||
vec[0] * _mtx[0][3] + vec[1] * _mtx[1][3] + vec[2] * _mtx[2][3] + _mtx[3][3]));
|
||||
}
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1. This is an overloaded method; it differs from the other version
|
||||
/// in that it returns a different value type.
|
||||
GfVec3f Transform(const GfVec3f &vec) const {
|
||||
return (GfProject(GfVec4f(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2],
|
||||
vec[0] * _mtx[0][3] + vec[1] * _mtx[1][3] + vec[2] * _mtx[2][3] + _mtx[3][3])));
|
||||
}
|
||||
|
||||
/// Transforms row vector \e vec by the matrix, returning the result. This
|
||||
/// treats the vector as a direction vector, so the translation
|
||||
/// information in the matrix is ignored. That is, it treats the vector as
|
||||
/// a 4-component vector whose fourth component is 0.
|
||||
GfVec3d TransformDir(const GfVec3d &vec) const {
|
||||
return GfVec3d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Transforms row vector \e vec by the matrix, returning the result. This
|
||||
/// treats the vector as a direction vector, so the translation
|
||||
/// information in the matrix is ignored. That is, it treats the vector as
|
||||
/// a 4-component vector whose fourth component is 0. This is an
|
||||
/// overloaded method; it differs from the other version in that it
|
||||
/// returns a different value type.
|
||||
GfVec3f TransformDir(const GfVec3f &vec) const {
|
||||
return GfVec3f(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2]);
|
||||
}
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1 and ignores the fourth column of the matrix (i.e. assumes it is
|
||||
/// (0, 0, 0, 1)).
|
||||
GfVec3d TransformAffine(const GfVec3d &vec) const {
|
||||
return GfVec3d(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2]);
|
||||
}
|
||||
|
||||
/// Transforms the row vector \e vec by the matrix, returning the result.
|
||||
/// This treats the vector as a 4-component vector whose fourth component
|
||||
/// is 1 and ignores the fourth column of the matrix (i.e. assumes it is
|
||||
/// (0, 0, 0, 1)).
|
||||
GfVec3f TransformAffine(const GfVec3f &vec) const {
|
||||
return GfVec3f(
|
||||
vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
|
||||
vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
|
||||
vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2]);
|
||||
}
|
||||
/// @}
|
||||
|
||||
private:
|
||||
/// Returns the determinant of the 3x3 submatrix specified by the three
|
||||
/// given row and column indices (0-3 for each).
|
||||
GF_API
|
||||
double _GetDeterminant3(size_t row1, size_t row2, size_t row3,
|
||||
size_t col1, size_t col2, size_t col3) const;
|
||||
|
||||
/// Diagonalizes the upper 3x3 matrix of a matrix known to be symmetric.
|
||||
void _Jacobi3(GfVec3d *eigenvalues, GfVec3d eigenvectors[3]) const;
|
||||
|
||||
/// Set the 3x3 submatrix to the rotation given by a quaternion,
|
||||
/// defined by the real component \p r and imaginary components \p i.
|
||||
void _SetRotateFromQuat(float r, const GfVec3f& i);
|
||||
|
||||
|
||||
private:
|
||||
/// Matrix storage, in row-major order.
|
||||
GfMatrixData<float, 4, 4> _mtx;
|
||||
|
||||
// Friend declarations
|
||||
friend class GfMatrix4d;
|
||||
};
|
||||
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// difference between each component of the matrix is less than or equal
|
||||
/// to \p tolerance, or false otherwise.
|
||||
GF_API
|
||||
bool GfIsClose(GfMatrix4f const &m1, GfMatrix4f const &m2, double tolerance);
|
||||
|
||||
/// Output a GfMatrix4f
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfMatrix4f const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX4F_H
|
||||
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_MATRIX_DATA_H
|
||||
#define PXR_BASE_GF_MATRIX_DATA_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfMatrixData
|
||||
///
|
||||
/// A class template used by GfMatrixXX to store values.
|
||||
template <class T, int Rows, int Columns>
|
||||
class GfMatrixData {
|
||||
public:
|
||||
|
||||
/// Return a pointer to a \a row of data.
|
||||
T *operator[](int row) {
|
||||
return _data + (row * Columns);
|
||||
}
|
||||
|
||||
/// Return a const pointer to a \a row of data.
|
||||
T const *operator[](int row) const {
|
||||
return _data + (row * Columns);
|
||||
}
|
||||
|
||||
/// Return a pointer to the start of all the data.
|
||||
T *GetData() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
/// Return a const pointer to the start of all the data.
|
||||
T const *GetData() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T _data[Rows * Columns];
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MATRIX_DATA_H
|
||||
@@ -0,0 +1,183 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_MULTI_INTERVAL_H
|
||||
#define PXR_BASE_GF_MULTI_INTERVAL_H
|
||||
|
||||
/// \file gf/multiInterval.h
|
||||
/// \ingroup group_gf_BasicMath
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/interval.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfMultiInterval
|
||||
/// \ingroup group_gf_BasicMath
|
||||
///
|
||||
/// GfMultiInterval represents a subset of the real number line as an
|
||||
/// ordered set of non-intersecting GfIntervals.
|
||||
///
|
||||
class GfMultiInterval
|
||||
{
|
||||
public:
|
||||
typedef std::set<GfInterval> Set;
|
||||
typedef Set::const_iterator const_iterator;
|
||||
typedef Set::const_iterator iterator;
|
||||
|
||||
/// \name Constructors
|
||||
/// @{
|
||||
/// Constructs an empty multi-interval.
|
||||
GfMultiInterval() = default;
|
||||
/// Constructs an multi-interval with the single given interval.
|
||||
GF_API explicit GfMultiInterval(const GfInterval &i);
|
||||
/// Constructs an multi-interval containing the given input intervals.
|
||||
GF_API explicit GfMultiInterval(const std::vector<GfInterval> &intervals);
|
||||
/// @}
|
||||
|
||||
GF_API bool operator==(const GfMultiInterval &that) const { return _set == that._set; }
|
||||
GF_API bool operator!=(const GfMultiInterval &that) const { return !(*this == that); }
|
||||
GF_API bool operator<(const GfMultiInterval &that) const { return _set < that._set; }
|
||||
GF_API bool operator>=(const GfMultiInterval &that) const { return !(*this < that); }
|
||||
GF_API bool operator>(const GfMultiInterval &that) const { return (that < *this); }
|
||||
GF_API bool operator<=(const GfMultiInterval &that) const { return !(that < *this); }
|
||||
|
||||
|
||||
/// Hash value.
|
||||
/// Just a basic hash function, not particularly high quality.
|
||||
GF_API size_t Hash() const;
|
||||
|
||||
friend inline size_t hash_value(const GfMultiInterval &mi) {
|
||||
return mi.Hash();
|
||||
}
|
||||
|
||||
/// \name Accessors
|
||||
/// @{
|
||||
|
||||
/// Returns true if the multi-interval is empty.
|
||||
GF_API bool IsEmpty() const { return _set.empty(); }
|
||||
|
||||
/// Returns the number of intervals in the set.
|
||||
GF_API size_t GetSize() const { return _set.size(); }
|
||||
|
||||
/// Returns an interval bounding the entire multi-interval.
|
||||
/// Returns an empty interval if the multi-interval is empty.
|
||||
GF_API GfInterval GetBounds() const;
|
||||
|
||||
/// Returns true if the multi-interval contains the given value.
|
||||
GF_API bool Contains(double d) const;
|
||||
|
||||
/// Returns true if the multi-interval contains the given interval.
|
||||
GF_API bool Contains(const GfInterval & i) const;
|
||||
|
||||
/// Returns true if the multi-interval contains all the intervals in the
|
||||
/// given multi-interval.
|
||||
GF_API bool Contains(const GfMultiInterval & s) const;
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Mutation
|
||||
/// @{
|
||||
|
||||
/// Clear the multi-interval.
|
||||
GF_API void Clear() { _set.clear(); }
|
||||
|
||||
/// Add the given interval to the multi-interval.
|
||||
GF_API void Add( const GfInterval & i );
|
||||
/// Add the given multi-interval to the multi-interval.
|
||||
/// Sets this object to the union of the two sets.
|
||||
GF_API void Add( const GfMultiInterval &s );
|
||||
|
||||
/// Uses the given interval to extend the multi-interval in
|
||||
/// the interval arithmetic sense.
|
||||
GF_API void ArithmeticAdd( const GfInterval &i );
|
||||
|
||||
/// Remove the given interval from this multi-interval.
|
||||
GF_API void Remove( const GfInterval & i );
|
||||
/// Remove the given multi-interval from this multi-interval.
|
||||
GF_API void Remove( const GfMultiInterval &s );
|
||||
|
||||
GF_API void Intersect( const GfInterval & i );
|
||||
GF_API void Intersect( const GfMultiInterval &s );
|
||||
|
||||
/// Return the complement of this set.
|
||||
GF_API GfMultiInterval GetComplement() const;
|
||||
|
||||
/// @}
|
||||
|
||||
/// \name Iteration
|
||||
/// Only const iterators are returned. To maintain the invariants of
|
||||
/// the multi-interval, changes must be made via the public mutation API.
|
||||
/// @{
|
||||
|
||||
GF_API const_iterator begin() const { return _set.begin(); }
|
||||
GF_API const_iterator end() const { return _set.end(); }
|
||||
|
||||
/// Returns an iterator identifying the first (lowest) interval whose
|
||||
/// minimum value is >= x. If no such interval exists, returns end().
|
||||
GF_API const_iterator lower_bound( double x ) const;
|
||||
|
||||
/// Returns an iterator identifying the first (lowest) interval whose
|
||||
/// minimum value is > x. If no such interval exists, returns end().
|
||||
GF_API const_iterator upper_bound( double x ) const;
|
||||
|
||||
/// Returns an iterator identifying the first (loest) interval whose
|
||||
/// minimum value is > x. If no such interval exists, returns end().
|
||||
GF_API const_iterator GetNextNonContainingInterval( double x ) const;
|
||||
|
||||
/// Returns an iterator identifying the last (highest) interval whose
|
||||
/// maximum value is < x. If no such interval exists, returns end().
|
||||
GF_API const_iterator GetPriorNonContainingInterval( double x ) const;
|
||||
|
||||
/// Returns an iterator identifying the interval that contains x. If
|
||||
/// no interval contains x, then it returns end()
|
||||
GF_API const_iterator GetContainingInterval( double x ) const;
|
||||
|
||||
/// @}
|
||||
|
||||
/// Returns the full interval (-inf, inf).
|
||||
static GfMultiInterval GetFullInterval() {
|
||||
return GfMultiInterval(GfInterval::GetFullInterval());
|
||||
}
|
||||
|
||||
/// Swap two multi-intervals
|
||||
void swap(GfMultiInterval &other) { _set.swap(other._set); }
|
||||
|
||||
private:
|
||||
void _AssertInvariants() const;
|
||||
|
||||
Set _set;
|
||||
};
|
||||
|
||||
/// Output a GfMultiInterval
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream & operator<<(std::ostream &out, const GfMultiInterval &s);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_MULTI_INTERVAL_H
|
||||
@@ -0,0 +1,215 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_PLANE_H
|
||||
#define PXR_BASE_GF_PLANE_H
|
||||
|
||||
/// \file gf/plane.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange3d;
|
||||
class GfMatrix4d;
|
||||
class GfVec4d;
|
||||
|
||||
/// \class GfPlane
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 3-dimensional plane
|
||||
///
|
||||
/// This class represents a three-dimensional plane as a normal vector
|
||||
/// and the distance of the plane from the origin, measured along the
|
||||
/// normal. The plane can also be used to represent a half-space: the
|
||||
/// side of the plane in the direction of the normal.
|
||||
///
|
||||
class GfPlane
|
||||
{
|
||||
public:
|
||||
|
||||
/// The default constructor leaves the plane parameters undefined.
|
||||
GfPlane() {
|
||||
}
|
||||
|
||||
/// This constructor sets this to the plane perpendicular to \p normal and
|
||||
/// at \p distance units from the origin. The passed-in normal is
|
||||
/// normalized to unit length first.
|
||||
GfPlane(const GfVec3d &normal, double distanceToOrigin) {
|
||||
Set(normal, distanceToOrigin);
|
||||
}
|
||||
|
||||
/// This constructor sets this to the plane perpendicular to \p normal and
|
||||
/// that passes through \p point. The passed-in normal is normalized to
|
||||
/// unit length first.
|
||||
GfPlane(const GfVec3d &normal, const GfVec3d &point) {
|
||||
Set(normal, point);
|
||||
}
|
||||
|
||||
/// This constructor sets this to the plane that contains the three given
|
||||
/// points. The normal is constructed from the cross product of (\p p1 -
|
||||
/// \p p0) (\p p2 - \p p0). Results are undefined if the points are
|
||||
/// collinear.
|
||||
GfPlane(const GfVec3d &p0, const GfVec3d &p1, const GfVec3d &p2) {
|
||||
Set(p0, p1, p2);
|
||||
}
|
||||
|
||||
/// This constructor creates a plane given by the equation
|
||||
/// \p eqn[0] * x + \p eqn[1] * y + \p eqn[2] * z + \p eqn[3] = 0.
|
||||
GfPlane(const GfVec4d &eqn) {
|
||||
Set(eqn);
|
||||
}
|
||||
|
||||
/// Sets this to the plane perpendicular to \p normal and at \p distance
|
||||
/// units from the origin. The passed-in normal is normalized to unit
|
||||
/// length first.
|
||||
void Set(const GfVec3d &normal, double distanceToOrigin) {
|
||||
_normal = normal.GetNormalized();
|
||||
_distance = distanceToOrigin;
|
||||
}
|
||||
|
||||
/// This constructor sets this to the plane perpendicular to \p normal and
|
||||
/// that passes through \p point. The passed-in normal is normalized to
|
||||
/// unit length first.
|
||||
GF_API
|
||||
void Set(const GfVec3d &normal, const GfVec3d &point);
|
||||
|
||||
/// This constructor sets this to the plane that contains the three given
|
||||
/// points. The normal is constructed from the cross product of (\p p1 -
|
||||
/// \p p0) (\p p2 - \p p0). Results are undefined if the points are
|
||||
/// collinear.
|
||||
GF_API
|
||||
void Set(const GfVec3d &p0,
|
||||
const GfVec3d &p1,
|
||||
const GfVec3d &p2);
|
||||
|
||||
/// This method sets this to the plane given by the equation
|
||||
/// \p eqn[0] * x + \p eqn[1] * y + \p eqn[2] * z + \p eqn[3] = 0.
|
||||
GF_API
|
||||
void Set(const GfVec4d &eqn);
|
||||
|
||||
/// Returns the unit-length normal vector of the plane.
|
||||
const GfVec3d & GetNormal() const {
|
||||
return _normal;
|
||||
}
|
||||
|
||||
/// Returns the distance of the plane from the origin.
|
||||
double GetDistanceFromOrigin() const {
|
||||
return _distance;
|
||||
}
|
||||
|
||||
/// Give the coefficients of the equation of the plane. Suitable
|
||||
/// to OpenGL calls to set the clipping plane.
|
||||
GF_API
|
||||
GfVec4d GetEquation() const;
|
||||
|
||||
/// Component-wise equality test. The normals and distances must match
|
||||
/// exactly for planes to be considered equal.
|
||||
bool operator ==(const GfPlane &p) const {
|
||||
return (_normal == p._normal &&
|
||||
_distance == p._distance);
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The normals and distances must match
|
||||
/// exactly for planes to be considered equal.
|
||||
bool operator !=(const GfPlane &p) const {
|
||||
return ! (*this == p);
|
||||
}
|
||||
|
||||
/// Returns the distance of point \p from the plane. This distance will be
|
||||
/// positive if the point is on the side of the plane containing the
|
||||
/// normal.
|
||||
double GetDistance(const GfVec3d &p) const {
|
||||
return p * _normal - _distance;
|
||||
}
|
||||
|
||||
/// Return the projection of \p p onto the plane.
|
||||
GfVec3d Project(const GfVec3d& p) const {
|
||||
return p - GetDistance(p) * _normal;
|
||||
}
|
||||
|
||||
/// Transforms the plane by the given matrix.
|
||||
GF_API
|
||||
GfPlane & Transform(const GfMatrix4d &matrix);
|
||||
|
||||
/// Flip the plane normal (if necessary) so that \p p is in the positive
|
||||
/// halfspace.
|
||||
void Reorient(const GfVec3d& p) {
|
||||
if (GetDistance(p) < 0) {
|
||||
_normal = -_normal;
|
||||
_distance = -_distance;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns \c true if the given aligned bounding box is at least
|
||||
/// partially on the positive side (the one the normal points into) of the
|
||||
/// plane.
|
||||
GF_API
|
||||
bool IntersectsPositiveHalfSpace(const GfRange3d &box) const;
|
||||
|
||||
/// Returns true if the given point is on the plane or within its positive
|
||||
/// half space.
|
||||
bool IntersectsPositiveHalfSpace(const GfVec3d &pt) const {
|
||||
return GetDistance(pt) >= 0.0;
|
||||
}
|
||||
|
||||
private:
|
||||
/// The normal to the plane. Points in direction of half-space.
|
||||
GfVec3d _normal;
|
||||
|
||||
/// Distance from the plane to the origin.
|
||||
double _distance;
|
||||
};
|
||||
|
||||
/// Fits a plane to the given \p points. There must be at least three points in
|
||||
/// order to fit the plane; if the size of \p points is less than three, this
|
||||
/// issues a coding error.
|
||||
///
|
||||
/// If the \p points are all collinear, then no plane can be determined, and
|
||||
/// this function returns \c false. Otherwise, if the fitting is successful,
|
||||
/// it returns \c true and sets \p *fitPlane to the fitted plane. If \p points
|
||||
/// contains exactly three points, then the resulting plane is the exact plane
|
||||
/// defined by the three points. If \p points contains more than three points,
|
||||
/// then this function determines the best-fitting plane for the given points.
|
||||
/// The orientation of the plane normal is arbitrary with regards to the
|
||||
/// plane's positive and negative half-spaces; you can use GfPlane::Reorient()
|
||||
/// to flip the plane if necessary.
|
||||
///
|
||||
/// The current implementation uses linear least squares and thus defines
|
||||
/// "best-fitting" as minimizing the sum of the squares of the vertical
|
||||
/// distances between points and the plane surface.
|
||||
GF_API
|
||||
bool GfFitPlaneToPoints(const std::vector<GfVec3d>& points, GfPlane* fitPlane);
|
||||
|
||||
/// Output a GfPlane using the format [(nx ny nz) distance].
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream&, const GfPlane&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_PLANE_H
|
||||
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_PY_BUFFER_UTILS_H
|
||||
#define PXR_BASE_GF_PY_BUFFER_UTILS_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Format strings matching Python buffer proto / struct module scheme.
|
||||
|
||||
// This function template is explicitly instantiated for T =
|
||||
// bool, [unsigned] (char, short, int, long), half, float, and double.
|
||||
template <class T>
|
||||
char *Gf_GetPyBufferFmtFor();
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_PY_BUFFER_UTILS_H
|
||||
@@ -0,0 +1,289 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// quat.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_QUATD_H
|
||||
#define PXR_BASE_GF_QUATD_H
|
||||
|
||||
/// \file gf/quatd.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfQuat<class GfQuatd> { static const bool value = true; };
|
||||
|
||||
|
||||
/// Return the dot (inner) product of two quaternions.
|
||||
double GfDot(const GfQuatd& q1, const GfQuatd& q2);
|
||||
|
||||
|
||||
/// \class GfQuatd
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type: a quaternion, a complex number with a real coefficient and
|
||||
/// three imaginary coefficients, stored as a 3-vector.
|
||||
///
|
||||
class GfQuatd
|
||||
{
|
||||
public:
|
||||
typedef double ScalarType;
|
||||
typedef GfVec3d ImaginaryType;
|
||||
|
||||
/// Default constructor leaves the quaternion undefined.
|
||||
GfQuatd() {}
|
||||
|
||||
/// Initialize the real coefficient to \p realVal and the imaginary
|
||||
/// coefficients to zero.
|
||||
///
|
||||
/// Since quaternions typically must be normalized, reasonable values for
|
||||
/// \p realVal are -1, 0, or 1. Other values are legal but are likely to
|
||||
/// be meaningless.
|
||||
///
|
||||
explicit GfQuatd (double realVal) : _imaginary(0), _real(realVal) {}
|
||||
|
||||
/// Initialize the real and imaginary coefficients.
|
||||
GfQuatd(double real, double i, double j, double k)
|
||||
: _imaginary(i, j, k), _real(real)
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize the real and imaginary coefficients.
|
||||
GfQuatd(double real, const GfVec3d &imaginary)
|
||||
: _imaginary(imaginary), _real(real)
|
||||
{
|
||||
}
|
||||
|
||||
/// Implicitly convert from GfQuatf.
|
||||
GF_API
|
||||
GfQuatd(class GfQuatf const &other);
|
||||
/// Implicitly convert from GfQuath.
|
||||
GF_API
|
||||
GfQuatd(class GfQuath const &other);
|
||||
|
||||
/// Return the identity quaternion, with real coefficient 1 and an
|
||||
/// imaginary coefficients all zero.
|
||||
static GfQuatd GetIdentity() { return GfQuatd(1.0); }
|
||||
|
||||
/// Return the real coefficient.
|
||||
double GetReal() const { return _real; }
|
||||
|
||||
/// Set the real coefficient.
|
||||
void SetReal(double real) { _real = real; }
|
||||
|
||||
/// Return the imaginary coefficient.
|
||||
const GfVec3d &GetImaginary() const { return _imaginary; }
|
||||
|
||||
/// Set the imaginary coefficients.
|
||||
void SetImaginary(const GfVec3d &imaginary) {
|
||||
_imaginary = imaginary;
|
||||
}
|
||||
|
||||
/// Set the imaginary coefficients.
|
||||
void SetImaginary(double i, double j, double k) {
|
||||
_imaginary.Set(i, j, k);
|
||||
}
|
||||
|
||||
/// Return geometric length of this quaternion.
|
||||
double GetLength() const { return GfSqrt(_GetLengthSquared()); }
|
||||
|
||||
/// length of this quaternion is smaller than \p eps, return the identity
|
||||
/// quaternion.
|
||||
GfQuatd
|
||||
GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfQuatd ret(*this);
|
||||
ret.Normalize(eps);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Normalizes this quaternion in place to unit length, returning the
|
||||
/// length before normalization. If the length of this quaternion is
|
||||
/// smaller than \p eps, this sets the quaternion to identity.
|
||||
GF_API
|
||||
double Normalize(double eps = GF_MIN_VECTOR_LENGTH);
|
||||
|
||||
/// Return this quaternion's conjugate, which is the quaternion with the
|
||||
/// same real coefficient and negated imaginary coefficients.
|
||||
GfQuatd GetConjugate() const {
|
||||
return GfQuatd(GetReal(), -GetImaginary());
|
||||
}
|
||||
|
||||
/// Return this quaternion's inverse, or reciprocal. This is the
|
||||
/// quaternion's conjugate divided by it's squared length.
|
||||
GfQuatd GetInverse() const {
|
||||
return GetConjugate() / _GetLengthSquared();
|
||||
}
|
||||
|
||||
/// Transform the GfVec3d point. If the quaternion is normalized,
|
||||
/// the transformation is a rotation. Given a GfQuatd q, q.Transform(point)
|
||||
/// is equivalent to:
|
||||
///
|
||||
/// (q * GfQuatd(0, point) * q.GetInverse()).GetImaginary()
|
||||
///
|
||||
/// but is more efficient.
|
||||
GF_API
|
||||
GfVec3d Transform(const GfVec3d& point) const;
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(const GfQuatd &q) {
|
||||
size_t h = boost::hash<ScalarType>()(q.GetReal());
|
||||
boost::hash_combine(h, q.GetImaginary());
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Component-wise negation.
|
||||
GfQuatd operator-() const {
|
||||
return GfQuatd(-GetReal(), -GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion equality test. The real and imaginary parts
|
||||
/// must match exactly for quaternions to be considered equal.
|
||||
bool operator==(const GfQuatd &q) const {
|
||||
return (GetReal() == q.GetReal() &&
|
||||
GetImaginary() == q.GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion inequality test. The real and imaginary
|
||||
/// parts must match exactly for quaternions to be considered equal.
|
||||
bool operator!=(const GfQuatd &q) const {
|
||||
return !(*this == q);
|
||||
}
|
||||
|
||||
/// Post-multiply quaternion \p q into this quaternion.
|
||||
GF_API
|
||||
GfQuatd &operator *=(const GfQuatd &q);
|
||||
|
||||
/// Multiply this quaternion's coefficients by \p s.
|
||||
GfQuatd &operator *=(double s) {
|
||||
_real *= s;
|
||||
_imaginary *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Divide this quaternion's coefficients by \p s.
|
||||
GfQuatd &operator /=(double s) {
|
||||
_real /= s;
|
||||
_imaginary /= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Add quaternion \p q to this quaternion.
|
||||
GfQuatd &operator +=(const GfQuatd &q) {
|
||||
_real += q._real;
|
||||
_imaginary += q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise unary difference operator.
|
||||
GfQuatd &operator -=(const GfQuatd &q) {
|
||||
_real -= q._real;
|
||||
_imaginary -= q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise binary sum operator.
|
||||
friend GfQuatd
|
||||
operator +(const GfQuatd &q1, const GfQuatd &q2) {
|
||||
return GfQuatd(q1) += q2;
|
||||
}
|
||||
|
||||
/// Component-wise binary difference operator.
|
||||
friend GfQuatd
|
||||
operator -(const GfQuatd &q1, const GfQuatd &q2) {
|
||||
return GfQuatd(q1) -= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternions \p q1 and \p q2.
|
||||
friend GfQuatd
|
||||
operator *(const GfQuatd &q1, const GfQuatd &q2) {
|
||||
return GfQuatd(q1) *= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuatd
|
||||
operator *(const GfQuatd &q, double s) {
|
||||
return GfQuatd(q) *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuatd
|
||||
operator *(double s, const GfQuatd &q) {
|
||||
return GfQuatd(q) *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar 1 / \p s.
|
||||
friend GfQuatd
|
||||
operator /(const GfQuatd &q, double s) {
|
||||
return GfQuatd(q) /= s;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Imaginary part
|
||||
GfVec3d _imaginary;
|
||||
|
||||
/// Real part
|
||||
double _real;
|
||||
|
||||
/// Returns the square of the length
|
||||
double
|
||||
_GetLengthSquared() const {
|
||||
return GfDot(*this, *this);
|
||||
}
|
||||
};
|
||||
|
||||
/// Spherically linearly interpolate between \p q0 and \p q1.
|
||||
///
|
||||
/// If the interpolant \p alpha is zero, then the result is \p q0, while
|
||||
/// \p alpha of one yields \p q1.
|
||||
GF_API GfQuatd
|
||||
GfSlerp(double alpha, const GfQuatd& q0, const GfQuatd& q1);
|
||||
|
||||
GF_API GfQuatd
|
||||
GfSlerp(const GfQuatd& q0, const GfQuatd& q1, double alpha);
|
||||
|
||||
inline double
|
||||
GfDot(GfQuatd const &q1, GfQuatd const &q2) {
|
||||
return GfDot(q1.GetImaginary(), q2.GetImaginary()) +
|
||||
q1.GetReal()*q2.GetReal();
|
||||
}
|
||||
|
||||
/// Output a GfQuatd using the format (re, i, j, k)
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfQuatd const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_QUATD_H
|
||||
@@ -0,0 +1,244 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_QUATERNION_H
|
||||
#define PXR_BASE_GF_QUATERNION_H
|
||||
|
||||
/// \file gf/quaternion.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfQuaternion
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type: complex number with scalar real part and vector imaginary
|
||||
/// part.
|
||||
///
|
||||
/// This class represents a generalized complex number that has a scalar real
|
||||
/// part and a vector of three imaginary values. Quaternions are used by the
|
||||
/// \c GfRotation class to represent arbitrary-axis rotations.
|
||||
///
|
||||
class GfQuaternion
|
||||
{
|
||||
public:
|
||||
|
||||
/// The default constructor leaves the quaternion undefined.
|
||||
GfQuaternion() {
|
||||
}
|
||||
|
||||
/// This constructor initializes the real part to the argument and
|
||||
/// the imaginary parts to zero.
|
||||
///
|
||||
/// Since quaternions typically need to be normalized, the only reasonable
|
||||
/// values for \p realVal are -1, 0, or 1. Other values are legal but are
|
||||
/// likely to be meaningless.
|
||||
explicit GfQuaternion(int realVal)
|
||||
: _real(realVal), _imaginary(0)
|
||||
{
|
||||
}
|
||||
|
||||
/// This constructor initializes the real and imaginary parts.
|
||||
GfQuaternion(double real, const GfVec3d &imaginary)
|
||||
: _real(real), _imaginary(imaginary) {
|
||||
}
|
||||
|
||||
/// Sets the real part of the quaternion.
|
||||
void SetReal(double real) {
|
||||
_real = real;
|
||||
}
|
||||
|
||||
/// Sets the imaginary part of the quaternion.
|
||||
void SetImaginary(const GfVec3d &imaginary) {
|
||||
_imaginary = imaginary;
|
||||
}
|
||||
|
||||
/// Returns the real part of the quaternion.
|
||||
double GetReal() const {
|
||||
return _real;
|
||||
}
|
||||
|
||||
/// Returns the imaginary part of the quaternion.
|
||||
const GfVec3d & GetImaginary() const {
|
||||
return _imaginary;
|
||||
}
|
||||
|
||||
/// Returns the identity quaternion, which has a real part of 1 and
|
||||
/// an imaginary part of (0,0,0).
|
||||
static GfQuaternion GetIdentity() {
|
||||
return GfQuaternion(1.0, GfVec3d(0.0, 0.0, 0.0));
|
||||
}
|
||||
|
||||
/// Returns geometric length of this quaternion.
|
||||
GF_API
|
||||
double GetLength() const;
|
||||
|
||||
/// Returns a normalized (unit-length) version of this quaternion.
|
||||
/// direction as this. If the length of this quaternion is smaller than \p
|
||||
/// eps, this returns the identity quaternion.
|
||||
GF_API
|
||||
GfQuaternion GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const;
|
||||
|
||||
/// Normalizes this quaternion in place to unit length, returning the
|
||||
/// length before normalization. If the length of this quaternion is
|
||||
/// smaller than \p eps, this sets the quaternion to identity.
|
||||
GF_API
|
||||
double Normalize(double eps = GF_MIN_VECTOR_LENGTH);
|
||||
|
||||
/// Returns the inverse of this quaternion.
|
||||
GF_API
|
||||
GfQuaternion GetInverse() const;
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(const GfQuaternion &q) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, q.GetReal());
|
||||
boost::hash_combine(h, q.GetImaginary());
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Component-wise quaternion equality test. The real and imaginary parts
|
||||
/// must match exactly for quaternions to be considered equal.
|
||||
bool operator ==(const GfQuaternion &q) const {
|
||||
return (GetReal() == q.GetReal() &&
|
||||
GetImaginary() == q.GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion inequality test. The real and imaginary
|
||||
/// parts must match exactly for quaternions to be considered equal.
|
||||
bool operator !=(const GfQuaternion &q) const {
|
||||
return ! (*this == q);
|
||||
}
|
||||
|
||||
/// Post-multiplies quaternion \p q into this quaternion.
|
||||
GF_API
|
||||
GfQuaternion & operator *=(const GfQuaternion &q);
|
||||
|
||||
/// Scales this quaternion by \p s.
|
||||
GF_API
|
||||
GfQuaternion & operator *=(double s);
|
||||
|
||||
/// Scales this quaternion by 1 / \p s.
|
||||
GfQuaternion & operator /=(double s) {
|
||||
return (*this) *= 1.0 / s;
|
||||
}
|
||||
|
||||
/// Component-wise unary sum operator.
|
||||
GfQuaternion & operator +=(const GfQuaternion &q) {
|
||||
_real += q._real;
|
||||
_imaginary += q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise unary difference operator.
|
||||
GfQuaternion & operator -=(const GfQuaternion &q) {
|
||||
_real -= q._real;
|
||||
_imaginary -= q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise binary sum operator.
|
||||
friend GfQuaternion operator +(const GfQuaternion &q1,
|
||||
const GfQuaternion &q2) {
|
||||
GfQuaternion qt = q1;
|
||||
return qt += q2;
|
||||
}
|
||||
|
||||
/// Component-wise binary difference operator.
|
||||
friend GfQuaternion operator -(const GfQuaternion &q1,
|
||||
const GfQuaternion &q2) {
|
||||
GfQuaternion qt = q1;
|
||||
return qt -= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternions \p q1 and \p q2.
|
||||
friend GfQuaternion operator *(const GfQuaternion &q1,
|
||||
const GfQuaternion &q2) {
|
||||
GfQuaternion qt = q1;
|
||||
return qt *= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuaternion operator *(const GfQuaternion &q, double s) {
|
||||
GfQuaternion qt = q;
|
||||
return qt *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuaternion operator *(double s, const GfQuaternion &q) {
|
||||
GfQuaternion qt = q;
|
||||
return qt *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar 1 / \p s.
|
||||
friend GfQuaternion operator /(const GfQuaternion &q, double s) {
|
||||
GfQuaternion qt = q;
|
||||
return qt /= s;
|
||||
}
|
||||
|
||||
/// Spherically interpolate between \p q0 and \p q1.
|
||||
///
|
||||
/// If the interpolant \p alpha
|
||||
/// is zero, then the result is \p q0, while \p alpha of one yields
|
||||
/// \p q1.
|
||||
GF_API
|
||||
friend GfQuaternion GfSlerp(double alpha,
|
||||
const GfQuaternion& q0,
|
||||
const GfQuaternion& q1);
|
||||
|
||||
// TODO Remove this legacy alias/overload.
|
||||
friend GF_API GfQuaternion GfSlerp(const GfQuaternion& q0,
|
||||
const GfQuaternion& q1,
|
||||
double alpha);
|
||||
|
||||
private:
|
||||
/// Real part
|
||||
double _real;
|
||||
/// Imaginary part
|
||||
GfVec3d _imaginary;
|
||||
|
||||
/// Returns the square of the length
|
||||
double _GetLengthSquared() const {
|
||||
return (_real * _real + GfDot(_imaginary, _imaginary));
|
||||
}
|
||||
};
|
||||
|
||||
// Friend functions must be declared.
|
||||
GF_API GfQuaternion GfSlerp(double alpha, const GfQuaternion& q0, const GfQuaternion& q1);
|
||||
GF_API GfQuaternion GfSlerp(const GfQuaternion& q0, const GfQuaternion& q1, double alpha);
|
||||
|
||||
/// Output a GfQuaternion using the format (r + (x, y, z)).
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream& out, const GfQuaternion& q);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_QUATERNION_H
|
||||
@@ -0,0 +1,289 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// quat.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_QUATF_H
|
||||
#define PXR_BASE_GF_QUATF_H
|
||||
|
||||
/// \file gf/quatf.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfQuat<class GfQuatf> { static const bool value = true; };
|
||||
|
||||
|
||||
/// Return the dot (inner) product of two quaternions.
|
||||
float GfDot(const GfQuatf& q1, const GfQuatf& q2);
|
||||
|
||||
|
||||
/// \class GfQuatf
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type: a quaternion, a complex number with a real coefficient and
|
||||
/// three imaginary coefficients, stored as a 3-vector.
|
||||
///
|
||||
class GfQuatf
|
||||
{
|
||||
public:
|
||||
typedef float ScalarType;
|
||||
typedef GfVec3f ImaginaryType;
|
||||
|
||||
/// Default constructor leaves the quaternion undefined.
|
||||
GfQuatf() {}
|
||||
|
||||
/// Initialize the real coefficient to \p realVal and the imaginary
|
||||
/// coefficients to zero.
|
||||
///
|
||||
/// Since quaternions typically must be normalized, reasonable values for
|
||||
/// \p realVal are -1, 0, or 1. Other values are legal but are likely to
|
||||
/// be meaningless.
|
||||
///
|
||||
explicit GfQuatf (float realVal) : _imaginary(0), _real(realVal) {}
|
||||
|
||||
/// Initialize the real and imaginary coefficients.
|
||||
GfQuatf(float real, float i, float j, float k)
|
||||
: _imaginary(i, j, k), _real(real)
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize the real and imaginary coefficients.
|
||||
GfQuatf(float real, const GfVec3f &imaginary)
|
||||
: _imaginary(imaginary), _real(real)
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfQuatd.
|
||||
GF_API
|
||||
explicit GfQuatf(class GfQuatd const &other);
|
||||
/// Implicitly convert from GfQuath.
|
||||
GF_API
|
||||
GfQuatf(class GfQuath const &other);
|
||||
|
||||
/// Return the identity quaternion, with real coefficient 1 and an
|
||||
/// imaginary coefficients all zero.
|
||||
static GfQuatf GetIdentity() { return GfQuatf(1.0); }
|
||||
|
||||
/// Return the real coefficient.
|
||||
float GetReal() const { return _real; }
|
||||
|
||||
/// Set the real coefficient.
|
||||
void SetReal(float real) { _real = real; }
|
||||
|
||||
/// Return the imaginary coefficient.
|
||||
const GfVec3f &GetImaginary() const { return _imaginary; }
|
||||
|
||||
/// Set the imaginary coefficients.
|
||||
void SetImaginary(const GfVec3f &imaginary) {
|
||||
_imaginary = imaginary;
|
||||
}
|
||||
|
||||
/// Set the imaginary coefficients.
|
||||
void SetImaginary(float i, float j, float k) {
|
||||
_imaginary.Set(i, j, k);
|
||||
}
|
||||
|
||||
/// Return geometric length of this quaternion.
|
||||
float GetLength() const { return GfSqrt(_GetLengthSquared()); }
|
||||
|
||||
/// length of this quaternion is smaller than \p eps, return the identity
|
||||
/// quaternion.
|
||||
GfQuatf
|
||||
GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfQuatf ret(*this);
|
||||
ret.Normalize(eps);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Normalizes this quaternion in place to unit length, returning the
|
||||
/// length before normalization. If the length of this quaternion is
|
||||
/// smaller than \p eps, this sets the quaternion to identity.
|
||||
GF_API
|
||||
float Normalize(float eps = GF_MIN_VECTOR_LENGTH);
|
||||
|
||||
/// Return this quaternion's conjugate, which is the quaternion with the
|
||||
/// same real coefficient and negated imaginary coefficients.
|
||||
GfQuatf GetConjugate() const {
|
||||
return GfQuatf(GetReal(), -GetImaginary());
|
||||
}
|
||||
|
||||
/// Return this quaternion's inverse, or reciprocal. This is the
|
||||
/// quaternion's conjugate divided by it's squared length.
|
||||
GfQuatf GetInverse() const {
|
||||
return GetConjugate() / _GetLengthSquared();
|
||||
}
|
||||
|
||||
/// Transform the GfVec3f point. If the quaternion is normalized,
|
||||
/// the transformation is a rotation. Given a GfQuatf q, q.Transform(point)
|
||||
/// is equivalent to:
|
||||
///
|
||||
/// (q * GfQuatf(0, point) * q.GetInverse()).GetImaginary()
|
||||
///
|
||||
/// but is more efficient.
|
||||
GF_API
|
||||
GfVec3f Transform(const GfVec3f& point) const;
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(const GfQuatf &q) {
|
||||
size_t h = boost::hash<ScalarType>()(q.GetReal());
|
||||
boost::hash_combine(h, q.GetImaginary());
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Component-wise negation.
|
||||
GfQuatf operator-() const {
|
||||
return GfQuatf(-GetReal(), -GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion equality test. The real and imaginary parts
|
||||
/// must match exactly for quaternions to be considered equal.
|
||||
bool operator==(const GfQuatf &q) const {
|
||||
return (GetReal() == q.GetReal() &&
|
||||
GetImaginary() == q.GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion inequality test. The real and imaginary
|
||||
/// parts must match exactly for quaternions to be considered equal.
|
||||
bool operator!=(const GfQuatf &q) const {
|
||||
return !(*this == q);
|
||||
}
|
||||
|
||||
/// Post-multiply quaternion \p q into this quaternion.
|
||||
GF_API
|
||||
GfQuatf &operator *=(const GfQuatf &q);
|
||||
|
||||
/// Multiply this quaternion's coefficients by \p s.
|
||||
GfQuatf &operator *=(float s) {
|
||||
_real *= s;
|
||||
_imaginary *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Divide this quaternion's coefficients by \p s.
|
||||
GfQuatf &operator /=(float s) {
|
||||
_real /= s;
|
||||
_imaginary /= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Add quaternion \p q to this quaternion.
|
||||
GfQuatf &operator +=(const GfQuatf &q) {
|
||||
_real += q._real;
|
||||
_imaginary += q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise unary difference operator.
|
||||
GfQuatf &operator -=(const GfQuatf &q) {
|
||||
_real -= q._real;
|
||||
_imaginary -= q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise binary sum operator.
|
||||
friend GfQuatf
|
||||
operator +(const GfQuatf &q1, const GfQuatf &q2) {
|
||||
return GfQuatf(q1) += q2;
|
||||
}
|
||||
|
||||
/// Component-wise binary difference operator.
|
||||
friend GfQuatf
|
||||
operator -(const GfQuatf &q1, const GfQuatf &q2) {
|
||||
return GfQuatf(q1) -= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternions \p q1 and \p q2.
|
||||
friend GfQuatf
|
||||
operator *(const GfQuatf &q1, const GfQuatf &q2) {
|
||||
return GfQuatf(q1) *= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuatf
|
||||
operator *(const GfQuatf &q, float s) {
|
||||
return GfQuatf(q) *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuatf
|
||||
operator *(float s, const GfQuatf &q) {
|
||||
return GfQuatf(q) *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar 1 / \p s.
|
||||
friend GfQuatf
|
||||
operator /(const GfQuatf &q, float s) {
|
||||
return GfQuatf(q) /= s;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Imaginary part
|
||||
GfVec3f _imaginary;
|
||||
|
||||
/// Real part
|
||||
float _real;
|
||||
|
||||
/// Returns the square of the length
|
||||
float
|
||||
_GetLengthSquared() const {
|
||||
return GfDot(*this, *this);
|
||||
}
|
||||
};
|
||||
|
||||
/// Spherically linearly interpolate between \p q0 and \p q1.
|
||||
///
|
||||
/// If the interpolant \p alpha is zero, then the result is \p q0, while
|
||||
/// \p alpha of one yields \p q1.
|
||||
GF_API GfQuatf
|
||||
GfSlerp(double alpha, const GfQuatf& q0, const GfQuatf& q1);
|
||||
|
||||
GF_API GfQuatf
|
||||
GfSlerp(const GfQuatf& q0, const GfQuatf& q1, double alpha);
|
||||
|
||||
inline float
|
||||
GfDot(GfQuatf const &q1, GfQuatf const &q2) {
|
||||
return GfDot(q1.GetImaginary(), q2.GetImaginary()) +
|
||||
q1.GetReal()*q2.GetReal();
|
||||
}
|
||||
|
||||
/// Output a GfQuatd using the format (re, i, j, k)
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfQuatf const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_QUATF_H
|
||||
@@ -0,0 +1,290 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// quat.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_QUATH_H
|
||||
#define PXR_BASE_GF_QUATH_H
|
||||
|
||||
/// \file gf/quath.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/declare.h"
|
||||
#include "pxr/base/gf/vec3h.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/half.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
template <>
|
||||
struct GfIsGfQuat<class GfQuath> { static const bool value = true; };
|
||||
|
||||
|
||||
/// Return the dot (inner) product of two quaternions.
|
||||
GfHalf GfDot(const GfQuath& q1, const GfQuath& q2);
|
||||
|
||||
|
||||
/// \class GfQuath
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type: a quaternion, a complex number with a real coefficient and
|
||||
/// three imaginary coefficients, stored as a 3-vector.
|
||||
///
|
||||
class GfQuath
|
||||
{
|
||||
public:
|
||||
typedef GfHalf ScalarType;
|
||||
typedef GfVec3h ImaginaryType;
|
||||
|
||||
/// Default constructor leaves the quaternion undefined.
|
||||
GfQuath() {}
|
||||
|
||||
/// Initialize the real coefficient to \p realVal and the imaginary
|
||||
/// coefficients to zero.
|
||||
///
|
||||
/// Since quaternions typically must be normalized, reasonable values for
|
||||
/// \p realVal are -1, 0, or 1. Other values are legal but are likely to
|
||||
/// be meaningless.
|
||||
///
|
||||
explicit GfQuath (GfHalf realVal) : _imaginary(0), _real(realVal) {}
|
||||
|
||||
/// Initialize the real and imaginary coefficients.
|
||||
GfQuath(GfHalf real, GfHalf i, GfHalf j, GfHalf k)
|
||||
: _imaginary(i, j, k), _real(real)
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize the real and imaginary coefficients.
|
||||
GfQuath(GfHalf real, const GfVec3h &imaginary)
|
||||
: _imaginary(imaginary), _real(real)
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfQuatd.
|
||||
GF_API
|
||||
explicit GfQuath(class GfQuatd const &other);
|
||||
/// Construct from GfQuatf.
|
||||
GF_API
|
||||
explicit GfQuath(class GfQuatf const &other);
|
||||
|
||||
/// Return the identity quaternion, with real coefficient 1 and an
|
||||
/// imaginary coefficients all zero.
|
||||
static GfQuath GetIdentity() { return GfQuath(1.0); }
|
||||
|
||||
/// Return the real coefficient.
|
||||
GfHalf GetReal() const { return _real; }
|
||||
|
||||
/// Set the real coefficient.
|
||||
void SetReal(GfHalf real) { _real = real; }
|
||||
|
||||
/// Return the imaginary coefficient.
|
||||
const GfVec3h &GetImaginary() const { return _imaginary; }
|
||||
|
||||
/// Set the imaginary coefficients.
|
||||
void SetImaginary(const GfVec3h &imaginary) {
|
||||
_imaginary = imaginary;
|
||||
}
|
||||
|
||||
/// Set the imaginary coefficients.
|
||||
void SetImaginary(GfHalf i, GfHalf j, GfHalf k) {
|
||||
_imaginary.Set(i, j, k);
|
||||
}
|
||||
|
||||
/// Return geometric length of this quaternion.
|
||||
GfHalf GetLength() const { return GfSqrt(_GetLengthSquared()); }
|
||||
|
||||
/// length of this quaternion is smaller than \p eps, return the identity
|
||||
/// quaternion.
|
||||
GfQuath
|
||||
GetNormalized(GfHalf eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfQuath ret(*this);
|
||||
ret.Normalize(eps);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// Normalizes this quaternion in place to unit length, returning the
|
||||
/// length before normalization. If the length of this quaternion is
|
||||
/// smaller than \p eps, this sets the quaternion to identity.
|
||||
GF_API
|
||||
GfHalf Normalize(GfHalf eps = GF_MIN_VECTOR_LENGTH);
|
||||
|
||||
/// Return this quaternion's conjugate, which is the quaternion with the
|
||||
/// same real coefficient and negated imaginary coefficients.
|
||||
GfQuath GetConjugate() const {
|
||||
return GfQuath(GetReal(), -GetImaginary());
|
||||
}
|
||||
|
||||
/// Return this quaternion's inverse, or reciprocal. This is the
|
||||
/// quaternion's conjugate divided by it's squared length.
|
||||
GfQuath GetInverse() const {
|
||||
return GetConjugate() / _GetLengthSquared();
|
||||
}
|
||||
|
||||
/// Transform the GfVec3h point. If the quaternion is normalized,
|
||||
/// the transformation is a rotation. Given a GfQuath q, q.Transform(point)
|
||||
/// is equivalent to:
|
||||
///
|
||||
/// (q * GfQuath(0, point) * q.GetInverse()).GetImaginary()
|
||||
///
|
||||
/// but is more efficient.
|
||||
GF_API
|
||||
GfVec3h Transform(const GfVec3h& point) const;
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(const GfQuath &q) {
|
||||
size_t h = boost::hash<ScalarType>()(q.GetReal());
|
||||
boost::hash_combine(h, q.GetImaginary());
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Component-wise negation.
|
||||
GfQuath operator-() const {
|
||||
return GfQuath(-GetReal(), -GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion equality test. The real and imaginary parts
|
||||
/// must match exactly for quaternions to be considered equal.
|
||||
bool operator==(const GfQuath &q) const {
|
||||
return (GetReal() == q.GetReal() &&
|
||||
GetImaginary() == q.GetImaginary());
|
||||
}
|
||||
|
||||
/// Component-wise quaternion inequality test. The real and imaginary
|
||||
/// parts must match exactly for quaternions to be considered equal.
|
||||
bool operator!=(const GfQuath &q) const {
|
||||
return !(*this == q);
|
||||
}
|
||||
|
||||
/// Post-multiply quaternion \p q into this quaternion.
|
||||
GF_API
|
||||
GfQuath &operator *=(const GfQuath &q);
|
||||
|
||||
/// Multiply this quaternion's coefficients by \p s.
|
||||
GfQuath &operator *=(GfHalf s) {
|
||||
_real *= s;
|
||||
_imaginary *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Divide this quaternion's coefficients by \p s.
|
||||
GfQuath &operator /=(GfHalf s) {
|
||||
_real /= s;
|
||||
_imaginary /= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Add quaternion \p q to this quaternion.
|
||||
GfQuath &operator +=(const GfQuath &q) {
|
||||
_real += q._real;
|
||||
_imaginary += q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise unary difference operator.
|
||||
GfQuath &operator -=(const GfQuath &q) {
|
||||
_real -= q._real;
|
||||
_imaginary -= q._imaginary;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise binary sum operator.
|
||||
friend GfQuath
|
||||
operator +(const GfQuath &q1, const GfQuath &q2) {
|
||||
return GfQuath(q1) += q2;
|
||||
}
|
||||
|
||||
/// Component-wise binary difference operator.
|
||||
friend GfQuath
|
||||
operator -(const GfQuath &q1, const GfQuath &q2) {
|
||||
return GfQuath(q1) -= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternions \p q1 and \p q2.
|
||||
friend GfQuath
|
||||
operator *(const GfQuath &q1, const GfQuath &q2) {
|
||||
return GfQuath(q1) *= q2;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuath
|
||||
operator *(const GfQuath &q, GfHalf s) {
|
||||
return GfQuath(q) *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar \p s.
|
||||
friend GfQuath
|
||||
operator *(GfHalf s, const GfQuath &q) {
|
||||
return GfQuath(q) *= s;
|
||||
}
|
||||
|
||||
/// Returns the product of quaternion \p q and scalar 1 / \p s.
|
||||
friend GfQuath
|
||||
operator /(const GfQuath &q, GfHalf s) {
|
||||
return GfQuath(q) /= s;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Imaginary part
|
||||
GfVec3h _imaginary;
|
||||
|
||||
/// Real part
|
||||
GfHalf _real;
|
||||
|
||||
/// Returns the square of the length
|
||||
GfHalf
|
||||
_GetLengthSquared() const {
|
||||
return GfDot(*this, *this);
|
||||
}
|
||||
};
|
||||
|
||||
/// Spherically linearly interpolate between \p q0 and \p q1.
|
||||
///
|
||||
/// If the interpolant \p alpha is zero, then the result is \p q0, while
|
||||
/// \p alpha of one yields \p q1.
|
||||
GF_API GfQuath
|
||||
GfSlerp(double alpha, const GfQuath& q0, const GfQuath& q1);
|
||||
|
||||
GF_API GfQuath
|
||||
GfSlerp(const GfQuath& q0, const GfQuath& q1, double alpha);
|
||||
|
||||
inline GfHalf
|
||||
GfDot(GfQuath const &q1, GfQuath const &q2) {
|
||||
return GfDot(q1.GetImaginary(), q2.GetImaginary()) +
|
||||
q1.GetReal()*q2.GetReal();
|
||||
}
|
||||
|
||||
/// Output a GfQuatd using the format (re, i, j, k)
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfQuath const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_QUATH_H
|
||||
@@ -0,0 +1,358 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// range.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_RANGE1D_H
|
||||
#define PXR_BASE_GF_RANGE1D_H
|
||||
|
||||
/// \file gf/range1d.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange1d;
|
||||
class GfRange1f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfRange<class GfRange1d> { static const bool value = true; };
|
||||
|
||||
/// \class GfRange1d
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 1-dimensional floating point range.
|
||||
///
|
||||
/// This class represents a 1-dimensional range (or interval) All
|
||||
/// operations are component-wise and conform to interval mathematics. An
|
||||
/// empty range is one where max < min.
|
||||
/// The default empty is [FLT_MAX,-FLT_MAX]
|
||||
class GfRange1d
|
||||
{
|
||||
public:
|
||||
|
||||
/// Helper typedef.
|
||||
typedef double MinMaxType;
|
||||
|
||||
static const size_t dimension = 1;
|
||||
typedef MinMaxType ScalarType;
|
||||
|
||||
/// Sets the range to an empty interval
|
||||
// TODO check whether this can be deprecated.
|
||||
void inline SetEmpty() {
|
||||
_min = FLT_MAX;
|
||||
_max = -FLT_MAX;
|
||||
}
|
||||
|
||||
/// The default constructor creates an empty range.
|
||||
GfRange1d() {
|
||||
SetEmpty();
|
||||
}
|
||||
|
||||
/// This constructor initializes the minimum and maximum points.
|
||||
GfRange1d(double min, double max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the minimum value of the range.
|
||||
double GetMin() const { return _min; }
|
||||
|
||||
/// Returns the maximum value of the range.
|
||||
double GetMax() const { return _max; }
|
||||
|
||||
/// Returns the size of the range.
|
||||
double GetSize() const { return _max - _min; }
|
||||
|
||||
/// Returns the midpoint of the range, that is, 0.5*(min+max).
|
||||
/// Note: this returns zero in the case of default-constructed ranges,
|
||||
/// or ranges set via SetEmpty().
|
||||
double GetMidpoint() const {
|
||||
return static_cast<ScalarType>(0.5) * _min
|
||||
+ static_cast<ScalarType>(0.5) * _max;
|
||||
}
|
||||
|
||||
/// Sets the minimum value of the range.
|
||||
void SetMin(double min) { _min = min; }
|
||||
|
||||
/// Sets the maximum value of the range.
|
||||
void SetMax(double max) { _max = max; }
|
||||
|
||||
/// Returns whether the range is empty (max < min).
|
||||
bool IsEmpty() const {
|
||||
return _min > _max;
|
||||
}
|
||||
|
||||
/// Modifies the range if necessary to surround the given value.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(double point) { UnionWith(point); }
|
||||
|
||||
/// Modifies the range if necessary to surround the given range.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfRange1d &range) { UnionWith(range); }
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
bool Contains(double point) const {
|
||||
return (point >= _min && point <= _max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool Contains(const GfRange1d &range) const {
|
||||
return Contains(range._min) && Contains(range._max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(double point) const {
|
||||
return Contains(point);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfRange1d &range) const {
|
||||
return Contains(range);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely outside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool IsOutside(const GfRange1d &range) const {
|
||||
return (range._max < _min || range._min > _max);
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange1d which contains both \p a and \p b.
|
||||
static GfRange1d GetUnion(const GfRange1d &a, const GfRange1d &b) {
|
||||
GfRange1d res = a;
|
||||
_FindMin(res._min,b._min);
|
||||
_FindMax(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange1d &UnionWith(const GfRange1d &b) {
|
||||
_FindMin(_min,b._min);
|
||||
_FindMax(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange1d &UnionWith(double b) {
|
||||
_FindMin(_min,b);
|
||||
_FindMax(_max,b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange1d which contains both \p a and \p b
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
static GfRange1d Union(const GfRange1d &a, const GfRange1d &b) {
|
||||
return GetUnion(a, b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange1d &Union(const GfRange1d &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange1d &Union(double b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange1d that describes the intersection of \p a and \p b.
|
||||
static GfRange1d GetIntersection(const GfRange1d &a, const GfRange1d &b) {
|
||||
GfRange1d res = a;
|
||||
_FindMax(res._min,b._min);
|
||||
_FindMin(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange1d that describes the intersection of \p a and \p b.
|
||||
/// \deprecated Use GetIntersection() instead.
|
||||
static GfRange1d Intersection(const GfRange1d &a, const GfRange1d &b) {
|
||||
return GetIntersection(a, b);
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result
|
||||
const GfRange1d &IntersectWith(const GfRange1d &b) {
|
||||
_FindMax(_min,b._min);
|
||||
_FindMin(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result.
|
||||
/// \deprecated Use IntersectWith() instead.
|
||||
const GfRange1d &Intersection(const GfRange1d &b) {
|
||||
return IntersectWith(b);
|
||||
}
|
||||
|
||||
/// unary sum.
|
||||
GfRange1d operator +=(const GfRange1d &b) {
|
||||
_min += b._min;
|
||||
_max += b._max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary difference.
|
||||
GfRange1d operator -=(const GfRange1d &b) {
|
||||
_min -= b._max;
|
||||
_max -= b._min;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary multiply.
|
||||
GfRange1d operator *=(double m) {
|
||||
if (m > 0) {
|
||||
_min *= m;
|
||||
_max *= m;
|
||||
} else {
|
||||
double tmp = _min;
|
||||
_min = _max * m;
|
||||
_max = tmp * m;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary division.
|
||||
GfRange1d operator /=(double m) {
|
||||
return *this *= (1.0 / m);
|
||||
}
|
||||
|
||||
/// binary sum.
|
||||
GfRange1d operator +(const GfRange1d &b) const {
|
||||
return GfRange1d(_min + b._min, _max + b._max);
|
||||
}
|
||||
|
||||
|
||||
/// binary difference.
|
||||
GfRange1d operator -(const GfRange1d &b) const {
|
||||
return GfRange1d(_min - b._max, _max - b._min);
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange1d operator *(double m, const GfRange1d &r) {
|
||||
return (m > 0 ?
|
||||
GfRange1d(r._min*m, r._max*m) :
|
||||
GfRange1d(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange1d operator *(const GfRange1d &r, double m) {
|
||||
return (m > 0 ?
|
||||
GfRange1d(r._min*m, r._max*m) :
|
||||
GfRange1d(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar divide.
|
||||
friend GfRange1d operator /(const GfRange1d &r, double m) {
|
||||
return r * (1.0 / m);
|
||||
}
|
||||
|
||||
/// hash.
|
||||
friend inline size_t hash_value(const GfRange1d &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// The min and max points must match exactly for equality.
|
||||
bool operator ==(const GfRange1d &b) const {
|
||||
return (_min == b._min && _max == b._max);
|
||||
}
|
||||
|
||||
bool operator !=(const GfRange1d &b) const {
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
/// Compare this range to a GfRange1f.
|
||||
///
|
||||
/// The values must match exactly and it does exactly what you might
|
||||
/// expect when comparing float and double values.
|
||||
GF_API inline bool operator ==(const GfRange1f& other) const;
|
||||
GF_API inline bool operator !=(const GfRange1f& other) const;
|
||||
|
||||
/// Compute the squared distance from a point to the range.
|
||||
GF_API
|
||||
double GetDistanceSquared(double p) const;
|
||||
|
||||
|
||||
private:
|
||||
/// Minimum and maximum points.
|
||||
double _min, _max;
|
||||
|
||||
/// Extends minimum point if necessary to contain given point.
|
||||
static void _FindMin(double &dest, double point) {
|
||||
if (point < dest) dest = point;
|
||||
}
|
||||
|
||||
/// Extends maximum point if necessary to contain given point.
|
||||
static void _FindMax(double &dest, double point) {
|
||||
if (point > dest) dest = point;
|
||||
}
|
||||
};
|
||||
|
||||
/// Output a GfRange1d.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfRange1d const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
#include "pxr/base/gf/range1f.h"
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline bool
|
||||
GfRange1d::operator ==(const GfRange1f& other) const {
|
||||
return _min == double(other.GetMin()) &&
|
||||
_max == double(other.GetMax());
|
||||
}
|
||||
|
||||
inline bool
|
||||
GfRange1d::operator !=(const GfRange1f& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RANGE1D_H
|
||||
@@ -0,0 +1,358 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// range.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_RANGE1F_H
|
||||
#define PXR_BASE_GF_RANGE1F_H
|
||||
|
||||
/// \file gf/range1f.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange1d;
|
||||
class GfRange1f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfRange<class GfRange1f> { static const bool value = true; };
|
||||
|
||||
/// \class GfRange1f
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 1-dimensional floating point range.
|
||||
///
|
||||
/// This class represents a 1-dimensional range (or interval) All
|
||||
/// operations are component-wise and conform to interval mathematics. An
|
||||
/// empty range is one where max < min.
|
||||
/// The default empty is [FLT_MAX,-FLT_MAX]
|
||||
class GfRange1f
|
||||
{
|
||||
public:
|
||||
|
||||
/// Helper typedef.
|
||||
typedef float MinMaxType;
|
||||
|
||||
static const size_t dimension = 1;
|
||||
typedef MinMaxType ScalarType;
|
||||
|
||||
/// Sets the range to an empty interval
|
||||
// TODO check whether this can be deprecated.
|
||||
void inline SetEmpty() {
|
||||
_min = FLT_MAX;
|
||||
_max = -FLT_MAX;
|
||||
}
|
||||
|
||||
/// The default constructor creates an empty range.
|
||||
GfRange1f() {
|
||||
SetEmpty();
|
||||
}
|
||||
|
||||
/// This constructor initializes the minimum and maximum points.
|
||||
GfRange1f(float min, float max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the minimum value of the range.
|
||||
float GetMin() const { return _min; }
|
||||
|
||||
/// Returns the maximum value of the range.
|
||||
float GetMax() const { return _max; }
|
||||
|
||||
/// Returns the size of the range.
|
||||
float GetSize() const { return _max - _min; }
|
||||
|
||||
/// Returns the midpoint of the range, that is, 0.5*(min+max).
|
||||
/// Note: this returns zero in the case of default-constructed ranges,
|
||||
/// or ranges set via SetEmpty().
|
||||
float GetMidpoint() const {
|
||||
return static_cast<ScalarType>(0.5) * _min
|
||||
+ static_cast<ScalarType>(0.5) * _max;
|
||||
}
|
||||
|
||||
/// Sets the minimum value of the range.
|
||||
void SetMin(float min) { _min = min; }
|
||||
|
||||
/// Sets the maximum value of the range.
|
||||
void SetMax(float max) { _max = max; }
|
||||
|
||||
/// Returns whether the range is empty (max < min).
|
||||
bool IsEmpty() const {
|
||||
return _min > _max;
|
||||
}
|
||||
|
||||
/// Modifies the range if necessary to surround the given value.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(float point) { UnionWith(point); }
|
||||
|
||||
/// Modifies the range if necessary to surround the given range.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfRange1f &range) { UnionWith(range); }
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
bool Contains(float point) const {
|
||||
return (point >= _min && point <= _max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool Contains(const GfRange1f &range) const {
|
||||
return Contains(range._min) && Contains(range._max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(float point) const {
|
||||
return Contains(point);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfRange1f &range) const {
|
||||
return Contains(range);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely outside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool IsOutside(const GfRange1f &range) const {
|
||||
return (range._max < _min || range._min > _max);
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange1f which contains both \p a and \p b.
|
||||
static GfRange1f GetUnion(const GfRange1f &a, const GfRange1f &b) {
|
||||
GfRange1f res = a;
|
||||
_FindMin(res._min,b._min);
|
||||
_FindMax(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange1f &UnionWith(const GfRange1f &b) {
|
||||
_FindMin(_min,b._min);
|
||||
_FindMax(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange1f &UnionWith(float b) {
|
||||
_FindMin(_min,b);
|
||||
_FindMax(_max,b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange1f which contains both \p a and \p b
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
static GfRange1f Union(const GfRange1f &a, const GfRange1f &b) {
|
||||
return GetUnion(a, b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange1f &Union(const GfRange1f &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange1f &Union(float b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange1f that describes the intersection of \p a and \p b.
|
||||
static GfRange1f GetIntersection(const GfRange1f &a, const GfRange1f &b) {
|
||||
GfRange1f res = a;
|
||||
_FindMax(res._min,b._min);
|
||||
_FindMin(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange1f that describes the intersection of \p a and \p b.
|
||||
/// \deprecated Use GetIntersection() instead.
|
||||
static GfRange1f Intersection(const GfRange1f &a, const GfRange1f &b) {
|
||||
return GetIntersection(a, b);
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result
|
||||
const GfRange1f &IntersectWith(const GfRange1f &b) {
|
||||
_FindMax(_min,b._min);
|
||||
_FindMin(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result.
|
||||
/// \deprecated Use IntersectWith() instead.
|
||||
const GfRange1f &Intersection(const GfRange1f &b) {
|
||||
return IntersectWith(b);
|
||||
}
|
||||
|
||||
/// unary sum.
|
||||
GfRange1f operator +=(const GfRange1f &b) {
|
||||
_min += b._min;
|
||||
_max += b._max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary difference.
|
||||
GfRange1f operator -=(const GfRange1f &b) {
|
||||
_min -= b._max;
|
||||
_max -= b._min;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary multiply.
|
||||
GfRange1f operator *=(double m) {
|
||||
if (m > 0) {
|
||||
_min *= m;
|
||||
_max *= m;
|
||||
} else {
|
||||
float tmp = _min;
|
||||
_min = _max * m;
|
||||
_max = tmp * m;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary division.
|
||||
GfRange1f operator /=(double m) {
|
||||
return *this *= (1.0 / m);
|
||||
}
|
||||
|
||||
/// binary sum.
|
||||
GfRange1f operator +(const GfRange1f &b) const {
|
||||
return GfRange1f(_min + b._min, _max + b._max);
|
||||
}
|
||||
|
||||
|
||||
/// binary difference.
|
||||
GfRange1f operator -(const GfRange1f &b) const {
|
||||
return GfRange1f(_min - b._max, _max - b._min);
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange1f operator *(double m, const GfRange1f &r) {
|
||||
return (m > 0 ?
|
||||
GfRange1f(r._min*m, r._max*m) :
|
||||
GfRange1f(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange1f operator *(const GfRange1f &r, double m) {
|
||||
return (m > 0 ?
|
||||
GfRange1f(r._min*m, r._max*m) :
|
||||
GfRange1f(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar divide.
|
||||
friend GfRange1f operator /(const GfRange1f &r, double m) {
|
||||
return r * (1.0 / m);
|
||||
}
|
||||
|
||||
/// hash.
|
||||
friend inline size_t hash_value(const GfRange1f &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// The min and max points must match exactly for equality.
|
||||
bool operator ==(const GfRange1f &b) const {
|
||||
return (_min == b._min && _max == b._max);
|
||||
}
|
||||
|
||||
bool operator !=(const GfRange1f &b) const {
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
/// Compare this range to a GfRange1d.
|
||||
///
|
||||
/// The values must match exactly and it does exactly what you might
|
||||
/// expect when comparing float and double values.
|
||||
GF_API inline bool operator ==(const GfRange1d& other) const;
|
||||
GF_API inline bool operator !=(const GfRange1d& other) const;
|
||||
|
||||
/// Compute the squared distance from a point to the range.
|
||||
GF_API
|
||||
double GetDistanceSquared(float p) const;
|
||||
|
||||
|
||||
private:
|
||||
/// Minimum and maximum points.
|
||||
float _min, _max;
|
||||
|
||||
/// Extends minimum point if necessary to contain given point.
|
||||
static void _FindMin(float &dest, float point) {
|
||||
if (point < dest) dest = point;
|
||||
}
|
||||
|
||||
/// Extends maximum point if necessary to contain given point.
|
||||
static void _FindMax(float &dest, float point) {
|
||||
if (point > dest) dest = point;
|
||||
}
|
||||
};
|
||||
|
||||
/// Output a GfRange1f.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfRange1f const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
#include "pxr/base/gf/range1d.h"
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline bool
|
||||
GfRange1f::operator ==(const GfRange1d& other) const {
|
||||
return _min == float(other.GetMin()) &&
|
||||
_max == float(other.GetMax());
|
||||
}
|
||||
|
||||
inline bool
|
||||
GfRange1f::operator !=(const GfRange1d& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RANGE1F_H
|
||||
@@ -0,0 +1,377 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// range.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_RANGE2D_H
|
||||
#define PXR_BASE_GF_RANGE2D_H
|
||||
|
||||
/// \file gf/range2d.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/vec2f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange2d;
|
||||
class GfRange2f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfRange<class GfRange2d> { static const bool value = true; };
|
||||
|
||||
/// \class GfRange2d
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 2-dimensional floating point range.
|
||||
///
|
||||
/// This class represents a 2-dimensional range (or interval) All
|
||||
/// operations are component-wise and conform to interval mathematics. An
|
||||
/// empty range is one where max < min.
|
||||
/// The default empty is [FLT_MAX,-FLT_MAX]
|
||||
class GfRange2d
|
||||
{
|
||||
public:
|
||||
|
||||
/// Helper typedef.
|
||||
typedef GfVec2d MinMaxType;
|
||||
|
||||
static const size_t dimension = GfVec2d::dimension;
|
||||
typedef GfVec2d::ScalarType ScalarType;
|
||||
|
||||
/// Sets the range to an empty interval
|
||||
// TODO check whether this can be deprecated.
|
||||
void inline SetEmpty() {
|
||||
_min[0] = _min[1] = FLT_MAX;
|
||||
_max[0] = _max[1] = -FLT_MAX;
|
||||
}
|
||||
|
||||
/// The default constructor creates an empty range.
|
||||
GfRange2d() {
|
||||
SetEmpty();
|
||||
}
|
||||
|
||||
/// This constructor initializes the minimum and maximum points.
|
||||
GfRange2d(const GfVec2d &min, const GfVec2d &max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the minimum value of the range.
|
||||
const GfVec2d &GetMin() const { return _min; }
|
||||
|
||||
/// Returns the maximum value of the range.
|
||||
const GfVec2d &GetMax() const { return _max; }
|
||||
|
||||
/// Returns the size of the range.
|
||||
GfVec2d GetSize() const { return _max - _min; }
|
||||
|
||||
/// Returns the midpoint of the range, that is, 0.5*(min+max).
|
||||
/// Note: this returns zero in the case of default-constructed ranges,
|
||||
/// or ranges set via SetEmpty().
|
||||
GfVec2d GetMidpoint() const {
|
||||
return static_cast<ScalarType>(0.5) * _min
|
||||
+ static_cast<ScalarType>(0.5) * _max;
|
||||
}
|
||||
|
||||
/// Sets the minimum value of the range.
|
||||
void SetMin(const GfVec2d &min) { _min = min; }
|
||||
|
||||
/// Sets the maximum value of the range.
|
||||
void SetMax(const GfVec2d &max) { _max = max; }
|
||||
|
||||
/// Returns whether the range is empty (max < min).
|
||||
bool IsEmpty() const {
|
||||
return _min[0] > _max[0] || _min[1] > _max[1];
|
||||
}
|
||||
|
||||
/// Modifies the range if necessary to surround the given value.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfVec2d &point) { UnionWith(point); }
|
||||
|
||||
/// Modifies the range if necessary to surround the given range.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfRange2d &range) { UnionWith(range); }
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
bool Contains(const GfVec2d &point) const {
|
||||
return (point[0] >= _min[0] && point[0] <= _max[0]
|
||||
&& point[1] >= _min[1] && point[1] <= _max[1]);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool Contains(const GfRange2d &range) const {
|
||||
return Contains(range._min) && Contains(range._max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfVec2d &point) const {
|
||||
return Contains(point);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfRange2d &range) const {
|
||||
return Contains(range);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely outside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool IsOutside(const GfRange2d &range) const {
|
||||
return ((range._max[0] < _min[0] || range._min[0] > _max[0])
|
||||
|| (range._max[1] < _min[1] || range._min[1] > _max[1]));
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange2d which contains both \p a and \p b.
|
||||
static GfRange2d GetUnion(const GfRange2d &a, const GfRange2d &b) {
|
||||
GfRange2d res = a;
|
||||
_FindMin(res._min,b._min);
|
||||
_FindMax(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange2d &UnionWith(const GfRange2d &b) {
|
||||
_FindMin(_min,b._min);
|
||||
_FindMax(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange2d &UnionWith(const GfVec2d &b) {
|
||||
_FindMin(_min,b);
|
||||
_FindMax(_max,b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange2d which contains both \p a and \p b
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
static GfRange2d Union(const GfRange2d &a, const GfRange2d &b) {
|
||||
return GetUnion(a, b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange2d &Union(const GfRange2d &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange2d &Union(const GfVec2d &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange2d that describes the intersection of \p a and \p b.
|
||||
static GfRange2d GetIntersection(const GfRange2d &a, const GfRange2d &b) {
|
||||
GfRange2d res = a;
|
||||
_FindMax(res._min,b._min);
|
||||
_FindMin(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange2d that describes the intersection of \p a and \p b.
|
||||
/// \deprecated Use GetIntersection() instead.
|
||||
static GfRange2d Intersection(const GfRange2d &a, const GfRange2d &b) {
|
||||
return GetIntersection(a, b);
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result
|
||||
const GfRange2d &IntersectWith(const GfRange2d &b) {
|
||||
_FindMax(_min,b._min);
|
||||
_FindMin(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result.
|
||||
/// \deprecated Use IntersectWith() instead.
|
||||
const GfRange2d &Intersection(const GfRange2d &b) {
|
||||
return IntersectWith(b);
|
||||
}
|
||||
|
||||
/// unary sum.
|
||||
GfRange2d operator +=(const GfRange2d &b) {
|
||||
_min += b._min;
|
||||
_max += b._max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary difference.
|
||||
GfRange2d operator -=(const GfRange2d &b) {
|
||||
_min -= b._max;
|
||||
_max -= b._min;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary multiply.
|
||||
GfRange2d operator *=(double m) {
|
||||
if (m > 0) {
|
||||
_min *= m;
|
||||
_max *= m;
|
||||
} else {
|
||||
GfVec2d tmp = _min;
|
||||
_min = _max * m;
|
||||
_max = tmp * m;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary division.
|
||||
GfRange2d operator /=(double m) {
|
||||
return *this *= (1.0 / m);
|
||||
}
|
||||
|
||||
/// binary sum.
|
||||
GfRange2d operator +(const GfRange2d &b) const {
|
||||
return GfRange2d(_min + b._min, _max + b._max);
|
||||
}
|
||||
|
||||
|
||||
/// binary difference.
|
||||
GfRange2d operator -(const GfRange2d &b) const {
|
||||
return GfRange2d(_min - b._max, _max - b._min);
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange2d operator *(double m, const GfRange2d &r) {
|
||||
return (m > 0 ?
|
||||
GfRange2d(r._min*m, r._max*m) :
|
||||
GfRange2d(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange2d operator *(const GfRange2d &r, double m) {
|
||||
return (m > 0 ?
|
||||
GfRange2d(r._min*m, r._max*m) :
|
||||
GfRange2d(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar divide.
|
||||
friend GfRange2d operator /(const GfRange2d &r, double m) {
|
||||
return r * (1.0 / m);
|
||||
}
|
||||
|
||||
/// hash.
|
||||
friend inline size_t hash_value(const GfRange2d &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// The min and max points must match exactly for equality.
|
||||
bool operator ==(const GfRange2d &b) const {
|
||||
return (_min == b._min && _max == b._max);
|
||||
}
|
||||
|
||||
bool operator !=(const GfRange2d &b) const {
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
/// Compare this range to a GfRange2f.
|
||||
///
|
||||
/// The values must match exactly and it does exactly what you might
|
||||
/// expect when comparing float and double values.
|
||||
GF_API inline bool operator ==(const GfRange2f& other) const;
|
||||
GF_API inline bool operator !=(const GfRange2f& other) const;
|
||||
|
||||
/// Compute the squared distance from a point to the range.
|
||||
GF_API
|
||||
double GetDistanceSquared(const GfVec2d &p) const;
|
||||
|
||||
/// Returns the ith corner of the range, in the following order:
|
||||
/// SW, SE, NW, NE.
|
||||
GF_API
|
||||
GfVec2d GetCorner(size_t i) const;
|
||||
|
||||
/// Returns the ith quadrant of the range, in the following order:
|
||||
/// SW, SE, NW, NE.
|
||||
GF_API
|
||||
GfRange2d GetQuadrant(size_t i) const;
|
||||
|
||||
/// The unit square.
|
||||
GF_API
|
||||
static const GfRange2d UnitSquare;
|
||||
|
||||
private:
|
||||
/// Minimum and maximum points.
|
||||
GfVec2d _min, _max;
|
||||
|
||||
/// Extends minimum point if necessary to contain given point.
|
||||
static void _FindMin(GfVec2d &dest, const GfVec2d &point) {
|
||||
if (point[0] < dest[0]) dest[0] = point[0];
|
||||
if (point[1] < dest[1]) dest[1] = point[1];
|
||||
}
|
||||
|
||||
/// Extends maximum point if necessary to contain given point.
|
||||
static void _FindMax(GfVec2d &dest, const GfVec2d &point) {
|
||||
if (point[0] > dest[0]) dest[0] = point[0];
|
||||
if (point[1] > dest[1]) dest[1] = point[1];
|
||||
}
|
||||
};
|
||||
|
||||
/// Output a GfRange2d.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfRange2d const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
#include "pxr/base/gf/range2f.h"
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline bool
|
||||
GfRange2d::operator ==(const GfRange2f& other) const {
|
||||
return _min == GfVec2d(other.GetMin()) &&
|
||||
_max == GfVec2d(other.GetMax());
|
||||
}
|
||||
|
||||
inline bool
|
||||
GfRange2d::operator !=(const GfRange2f& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RANGE2D_H
|
||||
@@ -0,0 +1,377 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// range.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_RANGE2F_H
|
||||
#define PXR_BASE_GF_RANGE2F_H
|
||||
|
||||
/// \file gf/range2f.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/vec2f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange2d;
|
||||
class GfRange2f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfRange<class GfRange2f> { static const bool value = true; };
|
||||
|
||||
/// \class GfRange2f
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 2-dimensional floating point range.
|
||||
///
|
||||
/// This class represents a 2-dimensional range (or interval) All
|
||||
/// operations are component-wise and conform to interval mathematics. An
|
||||
/// empty range is one where max < min.
|
||||
/// The default empty is [FLT_MAX,-FLT_MAX]
|
||||
class GfRange2f
|
||||
{
|
||||
public:
|
||||
|
||||
/// Helper typedef.
|
||||
typedef GfVec2f MinMaxType;
|
||||
|
||||
static const size_t dimension = GfVec2f::dimension;
|
||||
typedef GfVec2f::ScalarType ScalarType;
|
||||
|
||||
/// Sets the range to an empty interval
|
||||
// TODO check whether this can be deprecated.
|
||||
void inline SetEmpty() {
|
||||
_min[0] = _min[1] = FLT_MAX;
|
||||
_max[0] = _max[1] = -FLT_MAX;
|
||||
}
|
||||
|
||||
/// The default constructor creates an empty range.
|
||||
GfRange2f() {
|
||||
SetEmpty();
|
||||
}
|
||||
|
||||
/// This constructor initializes the minimum and maximum points.
|
||||
GfRange2f(const GfVec2f &min, const GfVec2f &max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the minimum value of the range.
|
||||
const GfVec2f &GetMin() const { return _min; }
|
||||
|
||||
/// Returns the maximum value of the range.
|
||||
const GfVec2f &GetMax() const { return _max; }
|
||||
|
||||
/// Returns the size of the range.
|
||||
GfVec2f GetSize() const { return _max - _min; }
|
||||
|
||||
/// Returns the midpoint of the range, that is, 0.5*(min+max).
|
||||
/// Note: this returns zero in the case of default-constructed ranges,
|
||||
/// or ranges set via SetEmpty().
|
||||
GfVec2f GetMidpoint() const {
|
||||
return static_cast<ScalarType>(0.5) * _min
|
||||
+ static_cast<ScalarType>(0.5) * _max;
|
||||
}
|
||||
|
||||
/// Sets the minimum value of the range.
|
||||
void SetMin(const GfVec2f &min) { _min = min; }
|
||||
|
||||
/// Sets the maximum value of the range.
|
||||
void SetMax(const GfVec2f &max) { _max = max; }
|
||||
|
||||
/// Returns whether the range is empty (max < min).
|
||||
bool IsEmpty() const {
|
||||
return _min[0] > _max[0] || _min[1] > _max[1];
|
||||
}
|
||||
|
||||
/// Modifies the range if necessary to surround the given value.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfVec2f &point) { UnionWith(point); }
|
||||
|
||||
/// Modifies the range if necessary to surround the given range.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfRange2f &range) { UnionWith(range); }
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
bool Contains(const GfVec2f &point) const {
|
||||
return (point[0] >= _min[0] && point[0] <= _max[0]
|
||||
&& point[1] >= _min[1] && point[1] <= _max[1]);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool Contains(const GfRange2f &range) const {
|
||||
return Contains(range._min) && Contains(range._max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfVec2f &point) const {
|
||||
return Contains(point);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfRange2f &range) const {
|
||||
return Contains(range);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely outside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool IsOutside(const GfRange2f &range) const {
|
||||
return ((range._max[0] < _min[0] || range._min[0] > _max[0])
|
||||
|| (range._max[1] < _min[1] || range._min[1] > _max[1]));
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange2f which contains both \p a and \p b.
|
||||
static GfRange2f GetUnion(const GfRange2f &a, const GfRange2f &b) {
|
||||
GfRange2f res = a;
|
||||
_FindMin(res._min,b._min);
|
||||
_FindMax(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange2f &UnionWith(const GfRange2f &b) {
|
||||
_FindMin(_min,b._min);
|
||||
_FindMax(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange2f &UnionWith(const GfVec2f &b) {
|
||||
_FindMin(_min,b);
|
||||
_FindMax(_max,b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange2f which contains both \p a and \p b
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
static GfRange2f Union(const GfRange2f &a, const GfRange2f &b) {
|
||||
return GetUnion(a, b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange2f &Union(const GfRange2f &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange2f &Union(const GfVec2f &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange2f that describes the intersection of \p a and \p b.
|
||||
static GfRange2f GetIntersection(const GfRange2f &a, const GfRange2f &b) {
|
||||
GfRange2f res = a;
|
||||
_FindMax(res._min,b._min);
|
||||
_FindMin(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange2f that describes the intersection of \p a and \p b.
|
||||
/// \deprecated Use GetIntersection() instead.
|
||||
static GfRange2f Intersection(const GfRange2f &a, const GfRange2f &b) {
|
||||
return GetIntersection(a, b);
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result
|
||||
const GfRange2f &IntersectWith(const GfRange2f &b) {
|
||||
_FindMax(_min,b._min);
|
||||
_FindMin(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result.
|
||||
/// \deprecated Use IntersectWith() instead.
|
||||
const GfRange2f &Intersection(const GfRange2f &b) {
|
||||
return IntersectWith(b);
|
||||
}
|
||||
|
||||
/// unary sum.
|
||||
GfRange2f operator +=(const GfRange2f &b) {
|
||||
_min += b._min;
|
||||
_max += b._max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary difference.
|
||||
GfRange2f operator -=(const GfRange2f &b) {
|
||||
_min -= b._max;
|
||||
_max -= b._min;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary multiply.
|
||||
GfRange2f operator *=(double m) {
|
||||
if (m > 0) {
|
||||
_min *= m;
|
||||
_max *= m;
|
||||
} else {
|
||||
GfVec2f tmp = _min;
|
||||
_min = _max * m;
|
||||
_max = tmp * m;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary division.
|
||||
GfRange2f operator /=(double m) {
|
||||
return *this *= (1.0 / m);
|
||||
}
|
||||
|
||||
/// binary sum.
|
||||
GfRange2f operator +(const GfRange2f &b) const {
|
||||
return GfRange2f(_min + b._min, _max + b._max);
|
||||
}
|
||||
|
||||
|
||||
/// binary difference.
|
||||
GfRange2f operator -(const GfRange2f &b) const {
|
||||
return GfRange2f(_min - b._max, _max - b._min);
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange2f operator *(double m, const GfRange2f &r) {
|
||||
return (m > 0 ?
|
||||
GfRange2f(r._min*m, r._max*m) :
|
||||
GfRange2f(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange2f operator *(const GfRange2f &r, double m) {
|
||||
return (m > 0 ?
|
||||
GfRange2f(r._min*m, r._max*m) :
|
||||
GfRange2f(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar divide.
|
||||
friend GfRange2f operator /(const GfRange2f &r, double m) {
|
||||
return r * (1.0 / m);
|
||||
}
|
||||
|
||||
/// hash.
|
||||
friend inline size_t hash_value(const GfRange2f &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// The min and max points must match exactly for equality.
|
||||
bool operator ==(const GfRange2f &b) const {
|
||||
return (_min == b._min && _max == b._max);
|
||||
}
|
||||
|
||||
bool operator !=(const GfRange2f &b) const {
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
/// Compare this range to a GfRange2d.
|
||||
///
|
||||
/// The values must match exactly and it does exactly what you might
|
||||
/// expect when comparing float and double values.
|
||||
GF_API inline bool operator ==(const GfRange2d& other) const;
|
||||
GF_API inline bool operator !=(const GfRange2d& other) const;
|
||||
|
||||
/// Compute the squared distance from a point to the range.
|
||||
GF_API
|
||||
double GetDistanceSquared(const GfVec2f &p) const;
|
||||
|
||||
/// Returns the ith corner of the range, in the following order:
|
||||
/// SW, SE, NW, NE.
|
||||
GF_API
|
||||
GfVec2f GetCorner(size_t i) const;
|
||||
|
||||
/// Returns the ith quadrant of the range, in the following order:
|
||||
/// SW, SE, NW, NE.
|
||||
GF_API
|
||||
GfRange2f GetQuadrant(size_t i) const;
|
||||
|
||||
/// The unit square.
|
||||
GF_API
|
||||
static const GfRange2f UnitSquare;
|
||||
|
||||
private:
|
||||
/// Minimum and maximum points.
|
||||
GfVec2f _min, _max;
|
||||
|
||||
/// Extends minimum point if necessary to contain given point.
|
||||
static void _FindMin(GfVec2f &dest, const GfVec2f &point) {
|
||||
if (point[0] < dest[0]) dest[0] = point[0];
|
||||
if (point[1] < dest[1]) dest[1] = point[1];
|
||||
}
|
||||
|
||||
/// Extends maximum point if necessary to contain given point.
|
||||
static void _FindMax(GfVec2f &dest, const GfVec2f &point) {
|
||||
if (point[0] > dest[0]) dest[0] = point[0];
|
||||
if (point[1] > dest[1]) dest[1] = point[1];
|
||||
}
|
||||
};
|
||||
|
||||
/// Output a GfRange2f.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfRange2f const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
#include "pxr/base/gf/range2d.h"
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline bool
|
||||
GfRange2f::operator ==(const GfRange2d& other) const {
|
||||
return _min == GfVec2f(other.GetMin()) &&
|
||||
_max == GfVec2f(other.GetMax());
|
||||
}
|
||||
|
||||
inline bool
|
||||
GfRange2f::operator !=(const GfRange2d& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RANGE2F_H
|
||||
@@ -0,0 +1,383 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// range.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_RANGE3D_H
|
||||
#define PXR_BASE_GF_RANGE3D_H
|
||||
|
||||
/// \file gf/range3d.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange3d;
|
||||
class GfRange3f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfRange<class GfRange3d> { static const bool value = true; };
|
||||
|
||||
/// \class GfRange3d
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 3-dimensional floating point range.
|
||||
///
|
||||
/// This class represents a 3-dimensional range (or interval) All
|
||||
/// operations are component-wise and conform to interval mathematics. An
|
||||
/// empty range is one where max < min.
|
||||
/// The default empty is [FLT_MAX,-FLT_MAX]
|
||||
class GfRange3d
|
||||
{
|
||||
public:
|
||||
|
||||
/// Helper typedef.
|
||||
typedef GfVec3d MinMaxType;
|
||||
|
||||
static const size_t dimension = GfVec3d::dimension;
|
||||
typedef GfVec3d::ScalarType ScalarType;
|
||||
|
||||
/// Sets the range to an empty interval
|
||||
// TODO check whether this can be deprecated.
|
||||
void inline SetEmpty() {
|
||||
_min[0] = _min[1] = _min[2] = FLT_MAX;
|
||||
_max[0] = _max[1] = _max[2] = -FLT_MAX;
|
||||
}
|
||||
|
||||
/// The default constructor creates an empty range.
|
||||
GfRange3d() {
|
||||
SetEmpty();
|
||||
}
|
||||
|
||||
/// This constructor initializes the minimum and maximum points.
|
||||
GfRange3d(const GfVec3d &min, const GfVec3d &max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the minimum value of the range.
|
||||
const GfVec3d &GetMin() const { return _min; }
|
||||
|
||||
/// Returns the maximum value of the range.
|
||||
const GfVec3d &GetMax() const { return _max; }
|
||||
|
||||
/// Returns the size of the range.
|
||||
GfVec3d GetSize() const { return _max - _min; }
|
||||
|
||||
/// Returns the midpoint of the range, that is, 0.5*(min+max).
|
||||
/// Note: this returns zero in the case of default-constructed ranges,
|
||||
/// or ranges set via SetEmpty().
|
||||
GfVec3d GetMidpoint() const {
|
||||
return static_cast<ScalarType>(0.5) * _min
|
||||
+ static_cast<ScalarType>(0.5) * _max;
|
||||
}
|
||||
|
||||
/// Sets the minimum value of the range.
|
||||
void SetMin(const GfVec3d &min) { _min = min; }
|
||||
|
||||
/// Sets the maximum value of the range.
|
||||
void SetMax(const GfVec3d &max) { _max = max; }
|
||||
|
||||
/// Returns whether the range is empty (max < min).
|
||||
bool IsEmpty() const {
|
||||
return _min[0] > _max[0] || _min[1] > _max[1] || _min[2] > _max[2];
|
||||
}
|
||||
|
||||
/// Modifies the range if necessary to surround the given value.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfVec3d &point) { UnionWith(point); }
|
||||
|
||||
/// Modifies the range if necessary to surround the given range.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfRange3d &range) { UnionWith(range); }
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
bool Contains(const GfVec3d &point) const {
|
||||
return (point[0] >= _min[0] && point[0] <= _max[0]
|
||||
&& point[1] >= _min[1] && point[1] <= _max[1]
|
||||
&& point[2] >= _min[2] && point[2] <= _max[2]);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool Contains(const GfRange3d &range) const {
|
||||
return Contains(range._min) && Contains(range._max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfVec3d &point) const {
|
||||
return Contains(point);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfRange3d &range) const {
|
||||
return Contains(range);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely outside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool IsOutside(const GfRange3d &range) const {
|
||||
return ((range._max[0] < _min[0] || range._min[0] > _max[0])
|
||||
|| (range._max[1] < _min[1] || range._min[1] > _max[1])
|
||||
|| (range._max[2] < _min[2] || range._min[2] > _max[2]));
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange3d which contains both \p a and \p b.
|
||||
static GfRange3d GetUnion(const GfRange3d &a, const GfRange3d &b) {
|
||||
GfRange3d res = a;
|
||||
_FindMin(res._min,b._min);
|
||||
_FindMax(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange3d &UnionWith(const GfRange3d &b) {
|
||||
_FindMin(_min,b._min);
|
||||
_FindMax(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange3d &UnionWith(const GfVec3d &b) {
|
||||
_FindMin(_min,b);
|
||||
_FindMax(_max,b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange3d which contains both \p a and \p b
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
static GfRange3d Union(const GfRange3d &a, const GfRange3d &b) {
|
||||
return GetUnion(a, b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange3d &Union(const GfRange3d &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange3d &Union(const GfVec3d &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange3d that describes the intersection of \p a and \p b.
|
||||
static GfRange3d GetIntersection(const GfRange3d &a, const GfRange3d &b) {
|
||||
GfRange3d res = a;
|
||||
_FindMax(res._min,b._min);
|
||||
_FindMin(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange3d that describes the intersection of \p a and \p b.
|
||||
/// \deprecated Use GetIntersection() instead.
|
||||
static GfRange3d Intersection(const GfRange3d &a, const GfRange3d &b) {
|
||||
return GetIntersection(a, b);
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result
|
||||
const GfRange3d &IntersectWith(const GfRange3d &b) {
|
||||
_FindMax(_min,b._min);
|
||||
_FindMin(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result.
|
||||
/// \deprecated Use IntersectWith() instead.
|
||||
const GfRange3d &Intersection(const GfRange3d &b) {
|
||||
return IntersectWith(b);
|
||||
}
|
||||
|
||||
/// unary sum.
|
||||
GfRange3d operator +=(const GfRange3d &b) {
|
||||
_min += b._min;
|
||||
_max += b._max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary difference.
|
||||
GfRange3d operator -=(const GfRange3d &b) {
|
||||
_min -= b._max;
|
||||
_max -= b._min;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary multiply.
|
||||
GfRange3d operator *=(double m) {
|
||||
if (m > 0) {
|
||||
_min *= m;
|
||||
_max *= m;
|
||||
} else {
|
||||
GfVec3d tmp = _min;
|
||||
_min = _max * m;
|
||||
_max = tmp * m;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary division.
|
||||
GfRange3d operator /=(double m) {
|
||||
return *this *= (1.0 / m);
|
||||
}
|
||||
|
||||
/// binary sum.
|
||||
GfRange3d operator +(const GfRange3d &b) const {
|
||||
return GfRange3d(_min + b._min, _max + b._max);
|
||||
}
|
||||
|
||||
|
||||
/// binary difference.
|
||||
GfRange3d operator -(const GfRange3d &b) const {
|
||||
return GfRange3d(_min - b._max, _max - b._min);
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange3d operator *(double m, const GfRange3d &r) {
|
||||
return (m > 0 ?
|
||||
GfRange3d(r._min*m, r._max*m) :
|
||||
GfRange3d(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange3d operator *(const GfRange3d &r, double m) {
|
||||
return (m > 0 ?
|
||||
GfRange3d(r._min*m, r._max*m) :
|
||||
GfRange3d(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar divide.
|
||||
friend GfRange3d operator /(const GfRange3d &r, double m) {
|
||||
return r * (1.0 / m);
|
||||
}
|
||||
|
||||
/// hash.
|
||||
friend inline size_t hash_value(const GfRange3d &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// The min and max points must match exactly for equality.
|
||||
bool operator ==(const GfRange3d &b) const {
|
||||
return (_min == b._min && _max == b._max);
|
||||
}
|
||||
|
||||
bool operator !=(const GfRange3d &b) const {
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
/// Compare this range to a GfRange3f.
|
||||
///
|
||||
/// The values must match exactly and it does exactly what you might
|
||||
/// expect when comparing float and double values.
|
||||
GF_API inline bool operator ==(const GfRange3f& other) const;
|
||||
GF_API inline bool operator !=(const GfRange3f& other) const;
|
||||
|
||||
/// Compute the squared distance from a point to the range.
|
||||
GF_API
|
||||
double GetDistanceSquared(const GfVec3d &p) const;
|
||||
|
||||
/// Returns the ith corner of the range, in the following order:
|
||||
/// LDB, RDB, LUB, RUB, LDF, RDF, LUF, RUF. Where L/R is left/right,
|
||||
/// D/U is down/up, and B/F is back/front.
|
||||
GF_API
|
||||
GfVec3d GetCorner(size_t i) const;
|
||||
|
||||
/// Returns the ith octant of the range, in the following order:
|
||||
/// LDB, RDB, LUB, RUB, LDF, RDF, LUF, RUF. Where L/R is left/right,
|
||||
/// D/U is down/up, and B/F is back/front.
|
||||
GF_API
|
||||
GfRange3d GetOctant(size_t i) const;
|
||||
|
||||
/// The unit cube.
|
||||
GF_API
|
||||
static const GfRange3d UnitCube;
|
||||
|
||||
private:
|
||||
/// Minimum and maximum points.
|
||||
GfVec3d _min, _max;
|
||||
|
||||
/// Extends minimum point if necessary to contain given point.
|
||||
static void _FindMin(GfVec3d &dest, const GfVec3d &point) {
|
||||
if (point[0] < dest[0]) dest[0] = point[0];
|
||||
if (point[1] < dest[1]) dest[1] = point[1];
|
||||
if (point[2] < dest[2]) dest[2] = point[2];
|
||||
}
|
||||
|
||||
/// Extends maximum point if necessary to contain given point.
|
||||
static void _FindMax(GfVec3d &dest, const GfVec3d &point) {
|
||||
if (point[0] > dest[0]) dest[0] = point[0];
|
||||
if (point[1] > dest[1]) dest[1] = point[1];
|
||||
if (point[2] > dest[2]) dest[2] = point[2];
|
||||
}
|
||||
};
|
||||
|
||||
/// Output a GfRange3d.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfRange3d const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
#include "pxr/base/gf/range3f.h"
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline bool
|
||||
GfRange3d::operator ==(const GfRange3f& other) const {
|
||||
return _min == GfVec3d(other.GetMin()) &&
|
||||
_max == GfVec3d(other.GetMax());
|
||||
}
|
||||
|
||||
inline bool
|
||||
GfRange3d::operator !=(const GfRange3f& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RANGE3D_H
|
||||
@@ -0,0 +1,383 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// range.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_RANGE3F_H
|
||||
#define PXR_BASE_GF_RANGE3F_H
|
||||
|
||||
/// \file gf/range3f.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cfloat>
|
||||
#include <cstddef>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfRange3d;
|
||||
class GfRange3f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfRange<class GfRange3f> { static const bool value = true; };
|
||||
|
||||
/// \class GfRange3f
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: 3-dimensional floating point range.
|
||||
///
|
||||
/// This class represents a 3-dimensional range (or interval) All
|
||||
/// operations are component-wise and conform to interval mathematics. An
|
||||
/// empty range is one where max < min.
|
||||
/// The default empty is [FLT_MAX,-FLT_MAX]
|
||||
class GfRange3f
|
||||
{
|
||||
public:
|
||||
|
||||
/// Helper typedef.
|
||||
typedef GfVec3f MinMaxType;
|
||||
|
||||
static const size_t dimension = GfVec3f::dimension;
|
||||
typedef GfVec3f::ScalarType ScalarType;
|
||||
|
||||
/// Sets the range to an empty interval
|
||||
// TODO check whether this can be deprecated.
|
||||
void inline SetEmpty() {
|
||||
_min[0] = _min[1] = _min[2] = FLT_MAX;
|
||||
_max[0] = _max[1] = _max[2] = -FLT_MAX;
|
||||
}
|
||||
|
||||
/// The default constructor creates an empty range.
|
||||
GfRange3f() {
|
||||
SetEmpty();
|
||||
}
|
||||
|
||||
/// This constructor initializes the minimum and maximum points.
|
||||
GfRange3f(const GfVec3f &min, const GfVec3f &max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns the minimum value of the range.
|
||||
const GfVec3f &GetMin() const { return _min; }
|
||||
|
||||
/// Returns the maximum value of the range.
|
||||
const GfVec3f &GetMax() const { return _max; }
|
||||
|
||||
/// Returns the size of the range.
|
||||
GfVec3f GetSize() const { return _max - _min; }
|
||||
|
||||
/// Returns the midpoint of the range, that is, 0.5*(min+max).
|
||||
/// Note: this returns zero in the case of default-constructed ranges,
|
||||
/// or ranges set via SetEmpty().
|
||||
GfVec3f GetMidpoint() const {
|
||||
return static_cast<ScalarType>(0.5) * _min
|
||||
+ static_cast<ScalarType>(0.5) * _max;
|
||||
}
|
||||
|
||||
/// Sets the minimum value of the range.
|
||||
void SetMin(const GfVec3f &min) { _min = min; }
|
||||
|
||||
/// Sets the maximum value of the range.
|
||||
void SetMax(const GfVec3f &max) { _max = max; }
|
||||
|
||||
/// Returns whether the range is empty (max < min).
|
||||
bool IsEmpty() const {
|
||||
return _min[0] > _max[0] || _min[1] > _max[1] || _min[2] > _max[2];
|
||||
}
|
||||
|
||||
/// Modifies the range if necessary to surround the given value.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfVec3f &point) { UnionWith(point); }
|
||||
|
||||
/// Modifies the range if necessary to surround the given range.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
void ExtendBy(const GfRange3f &range) { UnionWith(range); }
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
bool Contains(const GfVec3f &point) const {
|
||||
return (point[0] >= _min[0] && point[0] <= _max[0]
|
||||
&& point[1] >= _min[1] && point[1] <= _max[1]
|
||||
&& point[2] >= _min[2] && point[2] <= _max[2]);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool Contains(const GfRange3f &range) const {
|
||||
return Contains(range._min) && Contains(range._max);
|
||||
}
|
||||
|
||||
/// Returns true if the \p point is located inside the range. As with all
|
||||
/// operations of this type, the range is assumed to include its extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfVec3f &point) const {
|
||||
return Contains(point);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely inside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
/// \deprecated Use Contains() instead.
|
||||
bool IsInside(const GfRange3f &range) const {
|
||||
return Contains(range);
|
||||
}
|
||||
|
||||
/// Returns true if the \p range is located entirely outside the range. As
|
||||
/// with all operations of this type, the ranges are assumed to include
|
||||
/// their extrema.
|
||||
bool IsOutside(const GfRange3f &range) const {
|
||||
return ((range._max[0] < _min[0] || range._min[0] > _max[0])
|
||||
|| (range._max[1] < _min[1] || range._min[1] > _max[1])
|
||||
|| (range._max[2] < _min[2] || range._min[2] > _max[2]));
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange3f which contains both \p a and \p b.
|
||||
static GfRange3f GetUnion(const GfRange3f &a, const GfRange3f &b) {
|
||||
GfRange3f res = a;
|
||||
_FindMin(res._min,b._min);
|
||||
_FindMax(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange3f &UnionWith(const GfRange3f &b) {
|
||||
_FindMin(_min,b._min);
|
||||
_FindMax(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
const GfRange3f &UnionWith(const GfVec3f &b) {
|
||||
_FindMin(_min,b);
|
||||
_FindMax(_max,b);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the smallest \c GfRange3f which contains both \p a and \p b
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
static GfRange3f Union(const GfRange3f &a, const GfRange3f &b) {
|
||||
return GetUnion(a, b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange3f &Union(const GfRange3f &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Extend \p this to include \p b.
|
||||
/// \deprecated Use UnionWith() instead.
|
||||
const GfRange3f &Union(const GfVec3f &b) {
|
||||
return UnionWith(b);
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange3f that describes the intersection of \p a and \p b.
|
||||
static GfRange3f GetIntersection(const GfRange3f &a, const GfRange3f &b) {
|
||||
GfRange3f res = a;
|
||||
_FindMax(res._min,b._min);
|
||||
_FindMin(res._max,b._max);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Returns a \c GfRange3f that describes the intersection of \p a and \p b.
|
||||
/// \deprecated Use GetIntersection() instead.
|
||||
static GfRange3f Intersection(const GfRange3f &a, const GfRange3f &b) {
|
||||
return GetIntersection(a, b);
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result
|
||||
const GfRange3f &IntersectWith(const GfRange3f &b) {
|
||||
_FindMax(_min,b._min);
|
||||
_FindMin(_max,b._max);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Modifies this range to hold its intersection with \p b and returns the
|
||||
/// result.
|
||||
/// \deprecated Use IntersectWith() instead.
|
||||
const GfRange3f &Intersection(const GfRange3f &b) {
|
||||
return IntersectWith(b);
|
||||
}
|
||||
|
||||
/// unary sum.
|
||||
GfRange3f operator +=(const GfRange3f &b) {
|
||||
_min += b._min;
|
||||
_max += b._max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary difference.
|
||||
GfRange3f operator -=(const GfRange3f &b) {
|
||||
_min -= b._max;
|
||||
_max -= b._min;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary multiply.
|
||||
GfRange3f operator *=(double m) {
|
||||
if (m > 0) {
|
||||
_min *= m;
|
||||
_max *= m;
|
||||
} else {
|
||||
GfVec3f tmp = _min;
|
||||
_min = _max * m;
|
||||
_max = tmp * m;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// unary division.
|
||||
GfRange3f operator /=(double m) {
|
||||
return *this *= (1.0 / m);
|
||||
}
|
||||
|
||||
/// binary sum.
|
||||
GfRange3f operator +(const GfRange3f &b) const {
|
||||
return GfRange3f(_min + b._min, _max + b._max);
|
||||
}
|
||||
|
||||
|
||||
/// binary difference.
|
||||
GfRange3f operator -(const GfRange3f &b) const {
|
||||
return GfRange3f(_min - b._max, _max - b._min);
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange3f operator *(double m, const GfRange3f &r) {
|
||||
return (m > 0 ?
|
||||
GfRange3f(r._min*m, r._max*m) :
|
||||
GfRange3f(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar multiply.
|
||||
friend GfRange3f operator *(const GfRange3f &r, double m) {
|
||||
return (m > 0 ?
|
||||
GfRange3f(r._min*m, r._max*m) :
|
||||
GfRange3f(r._max*m, r._min*m));
|
||||
}
|
||||
|
||||
/// scalar divide.
|
||||
friend GfRange3f operator /(const GfRange3f &r, double m) {
|
||||
return r * (1.0 / m);
|
||||
}
|
||||
|
||||
/// hash.
|
||||
friend inline size_t hash_value(const GfRange3f &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// The min and max points must match exactly for equality.
|
||||
bool operator ==(const GfRange3f &b) const {
|
||||
return (_min == b._min && _max == b._max);
|
||||
}
|
||||
|
||||
bool operator !=(const GfRange3f &b) const {
|
||||
return !(*this == b);
|
||||
}
|
||||
|
||||
/// Compare this range to a GfRange3d.
|
||||
///
|
||||
/// The values must match exactly and it does exactly what you might
|
||||
/// expect when comparing float and double values.
|
||||
GF_API inline bool operator ==(const GfRange3d& other) const;
|
||||
GF_API inline bool operator !=(const GfRange3d& other) const;
|
||||
|
||||
/// Compute the squared distance from a point to the range.
|
||||
GF_API
|
||||
double GetDistanceSquared(const GfVec3f &p) const;
|
||||
|
||||
/// Returns the ith corner of the range, in the following order:
|
||||
/// LDB, RDB, LUB, RUB, LDF, RDF, LUF, RUF. Where L/R is left/right,
|
||||
/// D/U is down/up, and B/F is back/front.
|
||||
GF_API
|
||||
GfVec3f GetCorner(size_t i) const;
|
||||
|
||||
/// Returns the ith octant of the range, in the following order:
|
||||
/// LDB, RDB, LUB, RUB, LDF, RDF, LUF, RUF. Where L/R is left/right,
|
||||
/// D/U is down/up, and B/F is back/front.
|
||||
GF_API
|
||||
GfRange3f GetOctant(size_t i) const;
|
||||
|
||||
/// The unit cube.
|
||||
GF_API
|
||||
static const GfRange3f UnitCube;
|
||||
|
||||
private:
|
||||
/// Minimum and maximum points.
|
||||
GfVec3f _min, _max;
|
||||
|
||||
/// Extends minimum point if necessary to contain given point.
|
||||
static void _FindMin(GfVec3f &dest, const GfVec3f &point) {
|
||||
if (point[0] < dest[0]) dest[0] = point[0];
|
||||
if (point[1] < dest[1]) dest[1] = point[1];
|
||||
if (point[2] < dest[2]) dest[2] = point[2];
|
||||
}
|
||||
|
||||
/// Extends maximum point if necessary to contain given point.
|
||||
static void _FindMax(GfVec3f &dest, const GfVec3f &point) {
|
||||
if (point[0] > dest[0]) dest[0] = point[0];
|
||||
if (point[1] > dest[1]) dest[1] = point[1];
|
||||
if (point[2] > dest[2]) dest[2] = point[2];
|
||||
}
|
||||
};
|
||||
|
||||
/// Output a GfRange3f.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfRange3f const &);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
#include "pxr/base/gf/range3d.h"
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline bool
|
||||
GfRange3f::operator ==(const GfRange3d& other) const {
|
||||
return _min == GfVec3f(other.GetMin()) &&
|
||||
_max == GfVec3f(other.GetMax());
|
||||
}
|
||||
|
||||
inline bool
|
||||
GfRange3f::operator !=(const GfRange3d& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RANGE3F_H
|
||||
@@ -0,0 +1,291 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_RAY_H
|
||||
#define PXR_BASE_GF_RAY_H
|
||||
|
||||
/// \file gf/ray.h
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/matrix4d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <float.h>
|
||||
#include <limits>
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfBBox3d;
|
||||
class GfLine;
|
||||
class GfLineSeg;
|
||||
class GfPlane;
|
||||
class GfRange3d;
|
||||
|
||||
/// \class GfRay
|
||||
/// \ingroup group_gf_BasicGeometry
|
||||
///
|
||||
/// Basic type: Ray used for intersection testing
|
||||
///
|
||||
/// This class represents a three-dimensional ray in space, typically
|
||||
/// used for intersection testing. It consists of an origin and a
|
||||
/// direction.
|
||||
///
|
||||
/// Note that by default a \c GfRay does not normalize its direction
|
||||
/// vector to unit length.
|
||||
///
|
||||
/// Note for ray intersections, the start point is included in the computations,
|
||||
/// i.e., a distance of zero is defined to be intersecting.
|
||||
///
|
||||
class GfRay {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves the ray parameters undefined.
|
||||
GfRay() {
|
||||
}
|
||||
|
||||
/// This constructor takes a starting point and a direction.
|
||||
GfRay(const GfVec3d &startPoint, const GfVec3d &direction) {
|
||||
SetPointAndDirection(startPoint, direction);
|
||||
}
|
||||
|
||||
/// Sets the ray by specifying a starting point and a direction.
|
||||
GF_API
|
||||
void SetPointAndDirection(const GfVec3d &startPoint,
|
||||
const GfVec3d &direction);
|
||||
|
||||
/// Sets the ray by specifying a starting point and an ending point.
|
||||
GF_API
|
||||
void SetEnds(const GfVec3d &startPoint, const GfVec3d &endPoint);
|
||||
|
||||
/// Returns the starting point of the segment.
|
||||
const GfVec3d & GetStartPoint() const {
|
||||
return _startPoint;
|
||||
}
|
||||
|
||||
/// Returns the direction vector of the segment. This is not guaranteed to
|
||||
/// be unit length.
|
||||
const GfVec3d & GetDirection() const {
|
||||
return _direction;
|
||||
}
|
||||
|
||||
/// Returns the point that is \p distance units from the starting point
|
||||
/// along the direction vector, expressed in parametic distance.
|
||||
GfVec3d GetPoint(double distance) const {
|
||||
return _startPoint + distance * _direction;
|
||||
}
|
||||
|
||||
/// Transforms the ray by the given matrix.
|
||||
GF_API
|
||||
GfRay & Transform(const GfMatrix4d &matrix);
|
||||
|
||||
/// Returns the point on the ray that is closest to \p point. If \p
|
||||
/// rayDistance is not \c NULL, it will be set to the parametric distance
|
||||
/// along the ray of the closest point.
|
||||
GF_API
|
||||
GfVec3d FindClosestPoint(const GfVec3d &point,
|
||||
double *rayDistance = NULL) const;
|
||||
|
||||
/// Component-wise equality test. The starting points, directions, and
|
||||
/// lengths must match exactly for rays to be considered equal.
|
||||
bool operator ==(const GfRay &r) const {
|
||||
return (_startPoint == r._startPoint &&
|
||||
_direction == r._direction);
|
||||
}
|
||||
|
||||
/// Component-wise inequality test. The starting points, directions, and
|
||||
/// lengths must match exactly for rays to be considered equal.
|
||||
bool operator !=(const GfRay &r) const {
|
||||
return ! (*this == r);
|
||||
}
|
||||
|
||||
/// \name Intersection methods.
|
||||
///
|
||||
/// The methods in this group intersect the ray with a geometric entity.
|
||||
///
|
||||
///@{
|
||||
|
||||
/// Intersects the ray with the triangle formed by points \p p0, \p p1,
|
||||
/// and \p p2, returning \c true if it hits. If there is an intersection,
|
||||
/// it also returns the parametric distance to the intersection point in
|
||||
/// \p distance, the barycentric coordinates of the intersection point in
|
||||
/// \p barycentricCoords and the front-facing flag in \p frontFacing. The
|
||||
/// barycentric coordinates are defined with respect to the three vertices
|
||||
/// taken in order. The front-facing flag is \c true if the intersection
|
||||
/// hit the side of the triangle that is formed when the vertices are
|
||||
/// ordered counter-clockwise (right-hand rule). If any of the return
|
||||
/// pointers are \c NULL, the corresponding values are not returned.
|
||||
///
|
||||
/// If the distance to the intersection is greater than \p maxDist, then
|
||||
/// the method will return false.
|
||||
///
|
||||
/// Barycentric coordinates are defined to sum to 1 and satisfy this
|
||||
/// relationsip:
|
||||
/// \code
|
||||
/// intersectionPoint = (barycentricCoords[0] * p0 +
|
||||
/// barycentricCoords[1] * p1 +
|
||||
/// barycentricCoords[2] * p2);
|
||||
/// \endcode
|
||||
GF_API
|
||||
bool Intersect(const GfVec3d &p0,
|
||||
const GfVec3d &p1,
|
||||
const GfVec3d &p2,
|
||||
double *distance = NULL,
|
||||
GfVec3d *barycentricCoords = NULL,
|
||||
bool *frontFacing = NULL,
|
||||
double maxDist = std::numeric_limits<double>::infinity())
|
||||
const;
|
||||
|
||||
/// Intersects the ray with a plane, returning \c true if the ray is not
|
||||
/// parallel to the plane and the intersection is within the ray bounds.
|
||||
/// If there is an intersection, it also returns the parametric distance
|
||||
/// to the intersection point in \p distance and the front-facing flag in
|
||||
/// \p frontFacing, if they are not \c NULL. The front-facing flag is \c
|
||||
/// true if the intersection is on the side of the plane in which its
|
||||
/// normal points.
|
||||
GF_API
|
||||
bool Intersect(const GfPlane &plane, double *distance = NULL,
|
||||
bool *frontFacing = NULL) const;
|
||||
|
||||
/// Intersects the ray with an axis-aligned box, returning \c true if the
|
||||
/// ray intersects it at all within bounds. If there is an intersection,
|
||||
/// this also returns the parametric distances to the two intersection
|
||||
/// points in \p enterDistance and \p exitDistance.
|
||||
GF_API
|
||||
bool Intersect(const GfRange3d &box,
|
||||
double *enterDistance = NULL,
|
||||
double *exitDistance = NULL) const;
|
||||
|
||||
/// Intersects the ray with an oriented box, returning \c true if the
|
||||
/// ray intersects it at all within bounds. If there is an intersection,
|
||||
/// this also returns the parametric distances to the two intersection
|
||||
/// points in \p enterDistance and \p exitDistance.
|
||||
GF_API
|
||||
bool Intersect(const GfBBox3d &box,
|
||||
double *enterDistance = NULL,
|
||||
double *exitDistance = NULL) const;
|
||||
|
||||
/// Intersects the ray with a sphere, returning \c true if the ray
|
||||
/// intersects it at all within bounds. If there is an intersection,
|
||||
/// returns the parametric distance to the two intersection points in \p
|
||||
/// enterDistance and \p exitDistance.
|
||||
GF_API
|
||||
bool Intersect(const GfVec3d ¢er, double radius,
|
||||
double *enterDistance = NULL,
|
||||
double *exitDistance = NULL ) const;
|
||||
|
||||
/// Intersects the ray with an infinite cylinder, with axis \p axis,
|
||||
/// centered at the \p origin, with radius \p radius.
|
||||
///
|
||||
/// Returns \c true if the ray intersects it at all within bounds. If
|
||||
/// there is an intersection, returns the parametric distance to the two
|
||||
/// intersection points in \p enterDistance and \p exitDistance.
|
||||
///
|
||||
/// Note this method does not validate whether the radius is valid.
|
||||
GF_API
|
||||
bool Intersect(const GfVec3d &origin,
|
||||
const GfVec3d &axis,
|
||||
const double radius,
|
||||
double *enterDistance = NULL,
|
||||
double *exitDistance = NULL) const;
|
||||
|
||||
/// Intersects the ray with an infinite non-double cone, centered at \p
|
||||
/// origin, with axis \p axis, radius \p radius and apex at \p height.
|
||||
///
|
||||
/// Returns \c true if the ray intersects it at all within bounds. If
|
||||
/// there is an intersection, returns the parametric distance to the two
|
||||
/// intersection points in \p enterDistance and \p exitDistance.
|
||||
///
|
||||
/// Note this method does not validate whether the radius are height are
|
||||
/// valid.
|
||||
GF_API
|
||||
bool Intersect(const GfVec3d &origin,
|
||||
const GfVec3d &axis,
|
||||
const double radius,
|
||||
const double height,
|
||||
double *enterDistance = NULL,
|
||||
double *exitDistance = NULL) const;
|
||||
///@}
|
||||
|
||||
private:
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfRay &, const GfLine &,
|
||||
GfVec3d *, GfVec3d *,
|
||||
double *, double * );
|
||||
GF_API
|
||||
friend bool GfFindClosestPoints( const GfRay &, const GfLineSeg &,
|
||||
GfVec3d *, GfVec3d *,
|
||||
double *, double * );
|
||||
|
||||
/// Solves the quadratic equation returning the solutions, if defined, in
|
||||
/// \p enterDistance and \p exitDistance, where \p enterDistance is less
|
||||
/// than or equal to \p exitDistance.
|
||||
bool _SolveQuadratic(const double a,
|
||||
const double b,
|
||||
const double c,
|
||||
double *enterDistance = NULL,
|
||||
double *exitDistance = NULL) const;
|
||||
|
||||
/// The starting point of the ray.
|
||||
GfVec3d _startPoint;
|
||||
/// The direction vector.
|
||||
GfVec3d _direction;
|
||||
};
|
||||
|
||||
/// Computes the closest points between a ray and a line. The two points are
|
||||
/// returned in \p rayPoint and \p linePoint. The parametric distance of each
|
||||
/// point on the lines is returned in \p rayDistance and \p lineDistance.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints( const GfRay &ray, const GfLine &line,
|
||||
GfVec3d *rayPoint = nullptr,
|
||||
GfVec3d *linePoint = nullptr,
|
||||
double *rayDistance = nullptr,
|
||||
double *lineDistance = nullptr );
|
||||
|
||||
/// Computes the closest points between a ray and a line segment. The two
|
||||
/// points are returned in \p rayPoint and \p segPoint. The parametric
|
||||
/// distance of each point is returned in \p rayDistance and \p segDistance.
|
||||
///
|
||||
/// This returns \c false if the lines were close enough to parallel that no
|
||||
/// points could be computed; in this case, the other return values are
|
||||
/// undefined.
|
||||
GF_API
|
||||
bool GfFindClosestPoints( const GfRay &ray, const GfLineSeg &seg,
|
||||
GfVec3d *rayPoint = nullptr,
|
||||
GfVec3d *segPoint = nullptr,
|
||||
double *rayDistance = nullptr,
|
||||
double *segDistance = nullptr );
|
||||
|
||||
/// Output a GfRay using the format [(x y z) >> (x y z)].
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream&, const GfRay&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_RAY_H
|
||||
@@ -0,0 +1,309 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_RECT2I_H
|
||||
#define PXR_BASE_GF_RECT2I_H
|
||||
|
||||
/// \file gf/rect2i.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/vec2i.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfRect2i
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// A 2D rectangle with integer coordinates.
|
||||
///
|
||||
/// A rectangle is internally represented as two corners. We refer to these
|
||||
/// as the min and max corner where the min's x-coordinate and y-coordinate
|
||||
/// are assumed to be less than or equal to the max's corresponding coordinates.
|
||||
/// Normally, it is expressed as a min corner and a size.
|
||||
///
|
||||
/// Note that the max corner is included when computing the size (width and
|
||||
/// height) of a rectangle as the number of integral points in the x- and
|
||||
/// y-direction. In particular, if the min corner and max corner are the same,
|
||||
/// then the width and the height of the rectangle will both be one since we
|
||||
/// have exactly one integral point with coordinates greater or equal to the
|
||||
/// min corner and less or equal to the max corner.
|
||||
///
|
||||
/// Specifically, <em> width = maxX - minX + 1</em> and
|
||||
/// <em>height = maxY - minY + 1.</em>
|
||||
///
|
||||
class GfRect2i {
|
||||
public:
|
||||
/// Constructs an empty rectangle.
|
||||
GfRect2i(): _min(0,0), _max(-1,-1)
|
||||
{
|
||||
}
|
||||
|
||||
/// Constructs a rectangle with \p min and \p max corners.
|
||||
GfRect2i(const GfVec2i& min, const GfVec2i& max)
|
||||
: _min(min), _max(max)
|
||||
{
|
||||
}
|
||||
|
||||
/// Constructs a rectangle with \p min corner and the indicated \p width
|
||||
/// and \p height.
|
||||
GfRect2i(const GfVec2i& min, int width, int height)
|
||||
: _min(min), _max(min + GfVec2i(width-1, height-1))
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns true if the rectangle is a null rectangle.
|
||||
///
|
||||
/// A null rectangle has both the width and the height set to 0, that is
|
||||
/// \code
|
||||
/// GetMaxX() == GetMinX() - 1
|
||||
/// \endcode
|
||||
/// and
|
||||
/// \code
|
||||
/// GetMaxY() == GetMinY() - 1
|
||||
/// \endcode
|
||||
/// Remember that if \c GetMinX() and \c GetMaxX() return the same value
|
||||
/// then the rectangle has width 1, and similarly for the height.
|
||||
///
|
||||
/// A null rectangle is both empty, and not valid.
|
||||
bool IsNull() const {
|
||||
return GetWidth() == 0 && GetHeight() == 0;
|
||||
}
|
||||
|
||||
/// Returns true if the rectangle is empty.
|
||||
///
|
||||
/// An empty rectangle has one or both of its min coordinates strictly
|
||||
/// greater than the corresponding max coordinate.
|
||||
///
|
||||
/// An empty rectangle is not valid.
|
||||
bool IsEmpty() const {
|
||||
return GetWidth() <= 0 || GetHeight() <= 0;
|
||||
}
|
||||
|
||||
/// Return true if the rectangle is valid (equivalently, not empty).
|
||||
bool IsValid() const {
|
||||
return !IsEmpty();
|
||||
}
|
||||
|
||||
/// Returns a normalized rectangle, i.e. one that has a non-negative width
|
||||
/// and height.
|
||||
///
|
||||
/// \c GetNormalized() swaps the min and max x-coordinates to
|
||||
/// ensure a non-negative width, and similarly for the
|
||||
/// y-coordinates.
|
||||
GF_API
|
||||
GfRect2i GetNormalized() const;
|
||||
|
||||
/// Returns the min corner of the rectangle.
|
||||
const GfVec2i& GetMin() const {
|
||||
return _min;
|
||||
}
|
||||
|
||||
/// Returns the max corner of the rectangle.
|
||||
const GfVec2i& GetMax() const {
|
||||
return _max;
|
||||
}
|
||||
|
||||
/// Return the X value of min corner.
|
||||
///
|
||||
int GetMinX() const {
|
||||
return _min[0];
|
||||
}
|
||||
|
||||
/// Set the X value of the min corner.
|
||||
///
|
||||
void SetMinX(int x) {
|
||||
_min[0] = x;
|
||||
}
|
||||
|
||||
/// Return the X value of the max corner.
|
||||
///
|
||||
int GetMaxX() const {
|
||||
return _max[0];
|
||||
}
|
||||
|
||||
/// Set the X value of the max corner
|
||||
void SetMaxX(int x) {
|
||||
_max[0] = x;
|
||||
}
|
||||
|
||||
/// Return the Y value of the min corner
|
||||
///
|
||||
int GetMinY() const {
|
||||
return _min[1];
|
||||
}
|
||||
|
||||
/// Set the Y value of the min corner.
|
||||
///
|
||||
void SetMinY(int y) {
|
||||
_min[1] = y;
|
||||
}
|
||||
|
||||
/// Return the Y value of the max corner
|
||||
int GetMaxY() const {
|
||||
return _max[1];
|
||||
}
|
||||
|
||||
/// Set the Y value of the max corner
|
||||
void SetMaxY(int y) {
|
||||
_max[1] = y;
|
||||
}
|
||||
|
||||
/// Sets the min corner of the rectangle.
|
||||
void SetMin(const GfVec2i& min) {
|
||||
_min = min;
|
||||
}
|
||||
|
||||
/// Sets the max corner of the rectangle.
|
||||
void SetMax(const GfVec2i& max) {
|
||||
_max = max;
|
||||
}
|
||||
|
||||
/// Returns the center point of the rectangle.
|
||||
GfVec2i GetCenter() const {
|
||||
return (_min + _max) / 2;
|
||||
}
|
||||
|
||||
/// Move the rectangle by \p displ.
|
||||
void Translate(const GfVec2i& displacement) {
|
||||
_min += displacement;
|
||||
_max += displacement;
|
||||
}
|
||||
|
||||
/// Return the area of the rectangle.
|
||||
unsigned long GetArea() const {
|
||||
return (unsigned long)GetWidth() * (unsigned long)GetHeight();
|
||||
}
|
||||
|
||||
/// Returns the size of the rectangle as a vector (width,height).
|
||||
GfVec2i GetSize() const {
|
||||
return GfVec2i(GetWidth(), GetHeight());
|
||||
}
|
||||
|
||||
/// Returns the width of the rectangle.
|
||||
///
|
||||
/// \note If the min and max x-coordinates are coincident, the width is
|
||||
/// one.
|
||||
int GetWidth() const {
|
||||
return (_max[0] - _min[0]) + 1;
|
||||
}
|
||||
|
||||
/// Returns the height of the rectangle.
|
||||
///
|
||||
/// \note If the min and max y-coordinates are coincident, the height is
|
||||
/// one.
|
||||
int GetHeight() const {
|
||||
return (_max[1] - _min[1]) + 1;
|
||||
}
|
||||
|
||||
/// Computes the intersection of two rectangles.
|
||||
GfRect2i GetIntersection(const GfRect2i& that) const {
|
||||
if(IsEmpty())
|
||||
return *this;
|
||||
else if(that.IsEmpty())
|
||||
return that;
|
||||
else
|
||||
return GfRect2i(GfVec2i(GfMax(_min[0], that._min[0]),
|
||||
GfMax(_min[1], that._min[1])),
|
||||
GfVec2i(GfMin(_max[0], that._max[0]),
|
||||
GfMin(_max[1], that._max[1])));
|
||||
}
|
||||
|
||||
/// Computes the intersection of two rectangles.
|
||||
/// \deprecated Use GetIntersection() instead
|
||||
GfRect2i Intersect(const GfRect2i& that) const {
|
||||
return GetIntersection(that);
|
||||
}
|
||||
|
||||
/// Computes the union of two rectangles.
|
||||
GfRect2i GetUnion(const GfRect2i& that) const {
|
||||
if(IsEmpty())
|
||||
return that;
|
||||
else if(that.IsEmpty())
|
||||
return *this;
|
||||
else
|
||||
return GfRect2i(GfVec2i(GfMin(_min[0], that._min[0]),
|
||||
GfMin(_min[1], that._min[1])),
|
||||
GfVec2i(GfMax(_max[0], that._max[0]),
|
||||
GfMax(_max[1], that._max[1])));
|
||||
}
|
||||
|
||||
/// Computes the union of two rectangles
|
||||
/// \deprecated Use GetUnion() instead.
|
||||
GfRect2i Union(const GfRect2i& that) const {
|
||||
return GetUnion(that);
|
||||
}
|
||||
|
||||
/// Returns true if the specified point in the rectangle.
|
||||
bool Contains(const GfVec2i& p) const {
|
||||
return ((p[0] >= _min[0]) && (p[0] <= _max[0]) &&
|
||||
(p[1] >= _min[1]) && (p[1] <= _max[1]));
|
||||
}
|
||||
|
||||
friend inline size_t hash_value(const GfRect2i &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._min);
|
||||
boost::hash_combine(h, r._max);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Returns true if \p r1 and \p r2 are equal.
|
||||
friend bool operator==(const GfRect2i& r1, const GfRect2i& r2) {
|
||||
return r1._min == r2._min && r1._max == r2._max;
|
||||
}
|
||||
|
||||
/// Returns true if \p r1 and \p r2 are different.
|
||||
friend bool operator!=(const GfRect2i& r1, const GfRect2i& r2) {
|
||||
return !(r1 == r2);
|
||||
}
|
||||
|
||||
/// Computes the union of two rectangles.
|
||||
/// \see GetUnion()
|
||||
GfRect2i operator += (const GfRect2i& that) {
|
||||
*this = GetUnion(that);
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend GfRect2i operator + (const GfRect2i r1, const GfRect2i& r2) {
|
||||
GfRect2i tmp(r1);
|
||||
tmp += r2;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private:
|
||||
GfVec2i _min, _max;
|
||||
};
|
||||
|
||||
/// Output a GfRect2i using the format [(x y):(x y)].
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream&, const GfRect2i&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,290 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_ROTATION_H
|
||||
#define PXR_BASE_GF_ROTATION_H
|
||||
|
||||
/// \file gf/rotation.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/quaternion.h"
|
||||
#include "pxr/base/gf/quatd.h"
|
||||
#include "pxr/base/gf/matrix4d.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfRotation
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type: 3-space rotation specification.
|
||||
///
|
||||
/// This class represents a rotation in 3-space. This stores an axis as a
|
||||
/// normalized vector of 3 \c doubles and an angle in degrees (as a double).
|
||||
/// Rotations follow the right-hand rule: a positive rotation about an axis
|
||||
/// vector appears counter-clockwise when looking from the end of the vector
|
||||
/// toward the origin.
|
||||
///
|
||||
class GfRotation {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor leaves the rotation undefined.
|
||||
GfRotation() {
|
||||
}
|
||||
|
||||
/// This constructor initializes the rotation to be \p angle
|
||||
/// degrees about \p axis.
|
||||
GfRotation(const GfVec3d &axis, double angle) {
|
||||
SetAxisAngle(axis, angle);
|
||||
}
|
||||
|
||||
/// This constructor initializes the rotation from a quaternion.
|
||||
GfRotation(const GfQuaternion &quaternion) {
|
||||
SetQuaternion(quaternion);
|
||||
}
|
||||
|
||||
/// This constructor initializes the rotation from a quaternion. Note that
|
||||
/// this constructor accepts GfQuatf and GfQuath since they implicitly
|
||||
/// convert to GfQuatd.
|
||||
GfRotation(const GfQuatd &quat) { SetQuat(quat); }
|
||||
|
||||
/// This constructor initializes the rotation to one that brings
|
||||
/// the \p rotateFrom vector to align with \p rotateTo. The passed
|
||||
/// vectors need not be unit length.
|
||||
GF_API
|
||||
GfRotation(const GfVec3d &rotateFrom, const GfVec3d &rotateTo) {
|
||||
SetRotateInto(rotateFrom, rotateTo);
|
||||
}
|
||||
|
||||
/// Sets the rotation to be \p angle degrees about \p axis.
|
||||
GfRotation & SetAxisAngle(const GfVec3d &axis, double angle) {
|
||||
_axis = axis;
|
||||
_angle = angle;
|
||||
if (!GfIsClose(_axis * _axis, 1.0, 1e-10))
|
||||
_axis.Normalize();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Sets the rotation from a quaternion. Note that this method accepts
|
||||
/// GfQuatf and GfQuath since they implicitly convert to GfQuatd.
|
||||
GF_API
|
||||
GfRotation & SetQuat(const GfQuatd &quat);
|
||||
|
||||
/// Sets the rotation from a quaternion.
|
||||
GfRotation & SetQuaternion(const GfQuaternion &quat) {
|
||||
return SetQuat(GfQuatd(quat.GetReal(), quat.GetImaginary()));
|
||||
}
|
||||
|
||||
/// Sets the rotation to one that brings the \p rotateFrom vector
|
||||
/// to align with \p rotateTo. The passed vectors need not be unit
|
||||
/// length.
|
||||
GF_API
|
||||
GfRotation & SetRotateInto(const GfVec3d &rotateFrom,
|
||||
const GfVec3d &rotateTo);
|
||||
|
||||
/// Sets the rotation to an identity rotation.
|
||||
/// (This is chosen to be 0 degrees around the positive X axis.)
|
||||
GfRotation & SetIdentity() {
|
||||
_axis.Set(1.0, 0.0, 0.0);
|
||||
_angle = 0.0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the axis of rotation.
|
||||
const GfVec3d & GetAxis() const {
|
||||
return _axis;
|
||||
}
|
||||
|
||||
/// Returns the rotation angle in degrees.
|
||||
double GetAngle() const {
|
||||
return _angle;
|
||||
}
|
||||
|
||||
/// Returns the rotation expressed as a quaternion.
|
||||
GfQuaternion GetQuaternion() const {
|
||||
auto quat = GetQuat();
|
||||
return GfQuaternion(quat.GetReal(), quat.GetImaginary());
|
||||
}
|
||||
|
||||
/// Returns the rotation expressed as a quaternion.
|
||||
GF_API
|
||||
GfQuatd GetQuat() const;
|
||||
|
||||
/// Returns the inverse of this rotation.
|
||||
GfRotation GetInverse() const {
|
||||
return GfRotation(_axis, -_angle);
|
||||
}
|
||||
|
||||
/// Decompose rotation about 3 orthogonal axes.
|
||||
/// If the axes are not orthogonal, warnings will be spewed.
|
||||
GF_API
|
||||
GfVec3d Decompose( const GfVec3d &axis0,
|
||||
const GfVec3d &axis1,
|
||||
const GfVec3d &axis2 ) const;
|
||||
|
||||
// Full-featured method to Decompose a rotation matrix into Cardarian
|
||||
// angles.
|
||||
// Axes have must be normalized. If useHint is specified
|
||||
// then the current values stored within thetaTw, thetaFB, thetaLR,
|
||||
// and thetaSw will be treated as hint and used to help choose
|
||||
// an equivalent rotation that is as close as possible to the hints.
|
||||
//
|
||||
// One can use this routine to generate any combination of the three
|
||||
// angles by passing in NULL for the angle that is to be omitted.
|
||||
//
|
||||
// Passing in valid pointers for all four angles will decompose into
|
||||
// Tw, FB, and LR but allows Sw to be used for best matching of hint
|
||||
// values. It also allows an swShift value to be passed in as a
|
||||
// Sw that is applied after the rotation matrix to get a best fit rotation
|
||||
// in four angles.
|
||||
//
|
||||
// Angles are in radians.
|
||||
//
|
||||
// Specify \p handedness as -1.0 or 1.0, same as for MultiRotate.
|
||||
//
|
||||
// NOTE:
|
||||
// Geppetto math function Originally brought over to extMover
|
||||
// from //depot/main/tools/src/menv/lib/gpt/util.h [10/16/06]
|
||||
// And moved into GfRotation[12/1/08]. Updated for any
|
||||
// combination of three angles [12/1/11].
|
||||
//
|
||||
GF_API
|
||||
static void DecomposeRotation(const GfMatrix4d &rot,
|
||||
const GfVec3d &TwAxis,
|
||||
const GfVec3d &FBAxis,
|
||||
const GfVec3d &LRAxis,
|
||||
double handedness,
|
||||
double *thetaTw,
|
||||
double *thetaFB,
|
||||
double *thetaLR,
|
||||
double *thetaSw = NULL,
|
||||
bool useHint=false,
|
||||
const double *swShift=NULL);
|
||||
|
||||
// This function projects the vectors \p v1 and \p v2 onto the plane
|
||||
// normal to \p axis, and then returns the rotation about \p axis that
|
||||
// brings \p v1 onto \p v2.
|
||||
GF_API
|
||||
static GfRotation RotateOntoProjected(const GfVec3d &v1,
|
||||
const GfVec3d &v2,
|
||||
const GfVec3d &axis);
|
||||
|
||||
/// Transforms row vector \p vec by the rotation, returning the result.
|
||||
GF_API
|
||||
GfVec3f TransformDir( const GfVec3f &vec ) const;
|
||||
|
||||
/// \overload
|
||||
GF_API
|
||||
GfVec3d TransformDir( const GfVec3d &vec ) const;
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(const GfRotation &r) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, r._axis);
|
||||
boost::hash_combine(h, r._angle);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Component-wise rotation equality test. The axes and angles must match
|
||||
/// exactly for rotations to be considered equal. (To compare equality of
|
||||
/// the actual rotations, you can convert both to quaternions and test the
|
||||
/// results for equality.)
|
||||
bool operator ==(const GfRotation &r) const {
|
||||
return (_axis == r._axis &&
|
||||
_angle == r._angle);
|
||||
}
|
||||
|
||||
/// Component-wise rotation inequality test. The axes and angles must
|
||||
/// match exactly for rotations to be considered equal. (To compare
|
||||
/// equality of the actual rotations, you can convert both to quaternions
|
||||
/// and test the results for equality.)
|
||||
bool operator !=(const GfRotation &r) const {
|
||||
return ! (*this == r);
|
||||
}
|
||||
|
||||
/// Post-multiplies rotation \p r into this rotation.
|
||||
GF_API
|
||||
GfRotation & operator *=(const GfRotation &r);
|
||||
|
||||
/// Scales rotation angle by multiplying by \p scale.
|
||||
GfRotation & operator *=(double scale) {
|
||||
_angle *= scale;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Scales rotation angle by dividing by \p scale.
|
||||
GfRotation & operator /=(double scale) {
|
||||
_angle /= scale;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns composite rotation of rotations \p r1 and \p r2.
|
||||
friend GfRotation operator *(const GfRotation &r1,
|
||||
const GfRotation &r2) {
|
||||
GfRotation r = r1;
|
||||
return r *= r2;
|
||||
}
|
||||
|
||||
/// Returns a rotation equivalent to \p r with its angle multiplied
|
||||
/// by \p scale.
|
||||
friend GfRotation operator *(const GfRotation &r, double scale) {
|
||||
GfRotation rTmp = r;
|
||||
return rTmp *= scale;
|
||||
}
|
||||
|
||||
/// Returns a rotation equivalent to \p r with its angle multiplied
|
||||
/// by \p scale.
|
||||
friend GfRotation operator *(double scale, const GfRotation &r) {
|
||||
return (r * scale);
|
||||
}
|
||||
|
||||
/// Returns a rotation equivalent to \p r with its angle divided
|
||||
/// by \p scale.
|
||||
friend GfRotation operator /(const GfRotation &r, double scale) {
|
||||
GfRotation rTmp = r;
|
||||
return rTmp /= scale;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Axis storage.
|
||||
/// This axis is normalized to unit length whenever it is set.
|
||||
GfVec3d _axis;
|
||||
/// Angle storage (represented in degrees).
|
||||
double _angle;
|
||||
};
|
||||
|
||||
/// Output a GfRotation using the format [(x y z) a].
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream&, const GfRotation&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_ROTATION_H
|
||||
@@ -0,0 +1,200 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_SIZE2_H
|
||||
#define PXR_BASE_GF_SIZE2_H
|
||||
|
||||
/// \file gf/size2.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/inttypes.h"
|
||||
#include "pxr/base/gf/vec2i.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfSize2
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Two-dimensional array of sizes
|
||||
///
|
||||
/// GfSize2 is used to represent pairs of counts. It is based on the datatype
|
||||
/// size_t, and thus can only represent non-negative values in each dimension.
|
||||
/// If you need to represent negative numbers as well, use GfVec2i.
|
||||
///
|
||||
/// Usage of GfSize2 is similar to that of GfVec2i, except that all
|
||||
/// mathematical operations are componentwise (including multiplication).
|
||||
///
|
||||
class GfSize2 {
|
||||
public:
|
||||
/// Default constructor initializes components to zero.
|
||||
GfSize2() {
|
||||
Set(0, 0);
|
||||
}
|
||||
|
||||
/// Copy constructor.
|
||||
GfSize2(const GfSize2& o) {
|
||||
*this = o;
|
||||
}
|
||||
|
||||
/// Conversion from GfVec2i.
|
||||
explicit GfSize2(const GfVec2i&o) {
|
||||
Set(o[0], o[1]);
|
||||
}
|
||||
|
||||
/// Construct from an array.
|
||||
GfSize2(const size_t v[2]) {
|
||||
Set(v);
|
||||
}
|
||||
|
||||
/// Construct from two values.
|
||||
GfSize2(size_t v0, size_t v1) {
|
||||
Set(v0, v1);
|
||||
}
|
||||
|
||||
/// Set to the values in a given array.
|
||||
GfSize2 & Set(const size_t v[2]) {
|
||||
_vec[0] = v[0];
|
||||
_vec[1] = v[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set to values passed directly.
|
||||
GfSize2 & Set(size_t v0, size_t v1) {
|
||||
_vec[0] = v0;
|
||||
_vec[1] = v1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Array operator.
|
||||
size_t & operator [](size_t i) {
|
||||
return _vec[i];
|
||||
}
|
||||
|
||||
/// Const array operator.
|
||||
const size_t & operator [](size_t i) const {
|
||||
return _vec[i];
|
||||
}
|
||||
|
||||
/// Component-wise equality.
|
||||
bool operator ==(const GfSize2 &v) const {
|
||||
return _vec[0] == v._vec[0] && _vec[1] == v._vec[1];
|
||||
}
|
||||
|
||||
/// Component-wise inequality.
|
||||
bool operator !=(const GfSize2 &v) const {
|
||||
return ! (*this == v);
|
||||
}
|
||||
|
||||
/// Component-wise in-place addition.
|
||||
GfSize2 & operator +=(const GfSize2 &v) {
|
||||
_vec[0] += v._vec[0];
|
||||
_vec[1] += v._vec[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place subtraction.
|
||||
GfSize2 & operator -=(const GfSize2 &v) {
|
||||
_vec[0] -= v._vec[0];
|
||||
_vec[1] -= v._vec[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place multiplication.
|
||||
GfSize2 & operator *=(GfSize2 const &v) {
|
||||
_vec[0] *= v._vec[0];
|
||||
_vec[1] *= v._vec[1];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place multiplication by a scalar.
|
||||
GfSize2 & operator *=(int d) {
|
||||
_vec[0] = _vec[0] * d;
|
||||
_vec[1] = _vec[1] * d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place division by a scalar.
|
||||
GfSize2 & operator /=(int d) {
|
||||
_vec[0] = _vec[0] / d;
|
||||
_vec[1] = _vec[1] / d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise addition.
|
||||
friend GfSize2 operator +(const GfSize2 &v1, const GfSize2 &v2) {
|
||||
return GfSize2(v1._vec[0]+v2._vec[0],
|
||||
v1._vec[1]+v2._vec[1]);
|
||||
}
|
||||
|
||||
/// Component-wise subtraction.
|
||||
friend GfSize2 operator -(const GfSize2 &v1, const GfSize2 &v2) {
|
||||
return GfSize2(v1._vec[0]-v2._vec[0],
|
||||
v1._vec[1]-v2._vec[1]);
|
||||
}
|
||||
|
||||
/// Component-wise multiplication.
|
||||
friend GfSize2 operator *(const GfSize2 &v1, const GfSize2 &v2) {
|
||||
return GfSize2(v1._vec[0]*v2._vec[0],
|
||||
v1._vec[1]*v2._vec[1]);
|
||||
}
|
||||
|
||||
/// Component-wise multiplication by a scalar.
|
||||
friend GfSize2 operator *(const GfSize2 &v1, int s) {
|
||||
return GfSize2(v1._vec[0]*s,
|
||||
v1._vec[1]*s);
|
||||
}
|
||||
|
||||
/// Component-wise multiplication by a scalar.
|
||||
friend GfSize2 operator *(int s, const GfSize2 &v1) {
|
||||
return GfSize2(v1._vec[0]*s,
|
||||
v1._vec[1]*s);
|
||||
}
|
||||
|
||||
/// Component-wise division by a scalar.
|
||||
friend GfSize2 operator /(const GfSize2 &v1, int s) {
|
||||
return GfSize2(v1._vec[0]/s,
|
||||
v1._vec[1]/s);
|
||||
}
|
||||
|
||||
/// Output operator.
|
||||
GF_API
|
||||
friend std::ostream &operator<<(std::ostream &o, GfSize2 const &v);
|
||||
|
||||
/// Conversion to GfVec2i.
|
||||
operator GfVec2i() const {
|
||||
return GfVec2i(_vec[0], _vec[1]);
|
||||
}
|
||||
private:
|
||||
size_t _vec[2];
|
||||
};
|
||||
|
||||
// Friend functions must be declared
|
||||
GF_API std::ostream &operator<<(std::ostream &o, GfSize2 const &v);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_SIZE2_H
|
||||
@@ -0,0 +1,213 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_SIZE3_H
|
||||
#define PXR_BASE_GF_SIZE3_H
|
||||
|
||||
/// \file gf/size3.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/arch/inttypes.h"
|
||||
#include "pxr/base/gf/vec3i.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \class GfSize3
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Three-dimensional array of sizes
|
||||
///
|
||||
/// GfSize3 is used to represent triples of counts. It is based on the
|
||||
/// datatype size_t, and thus can only represent non-negative values in each
|
||||
/// dimension. If you need to represent negative numbers as well, use GfVeci.
|
||||
///
|
||||
/// Usage of GfSize3 is similar to that of GfVec3i, except that all
|
||||
/// mathematical operations are componentwise (including multiplication).
|
||||
///
|
||||
class GfSize3 {
|
||||
public:
|
||||
/// Default constructor initializes components to zero
|
||||
GfSize3() {
|
||||
Set(0, 0, 0);
|
||||
}
|
||||
|
||||
/// Copy constructor.
|
||||
GfSize3(const GfSize3& o) {
|
||||
*this = o;
|
||||
}
|
||||
|
||||
/// Conversion from GfVec3i
|
||||
explicit GfSize3(const GfVec3i&o) {
|
||||
Set(o[0], o[1], o[2]);
|
||||
}
|
||||
|
||||
/// Construct from an array
|
||||
GfSize3(const size_t v[3]) {
|
||||
Set(v);
|
||||
}
|
||||
|
||||
/// Construct from three values
|
||||
GfSize3(size_t v0, size_t v1, size_t v2) {
|
||||
Set(v0, v1, v2);
|
||||
}
|
||||
|
||||
/// Set to the values in \p v.
|
||||
GfSize3 & Set(const size_t v[3]) {
|
||||
_vec[0] = v[0];
|
||||
_vec[1] = v[1];
|
||||
_vec[2] = v[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set to values passed directly
|
||||
GfSize3 & Set(size_t v0, size_t v1, size_t v2) {
|
||||
_vec[0] = v0;
|
||||
_vec[1] = v1;
|
||||
_vec[2] = v2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Array operator
|
||||
size_t & operator [](size_t i) {
|
||||
return _vec[i];
|
||||
}
|
||||
|
||||
/// Const array operator
|
||||
const size_t & operator [](size_t i) const {
|
||||
return _vec[i];
|
||||
}
|
||||
|
||||
/// Component-wise equality
|
||||
bool operator ==(const GfSize3 &v) const {
|
||||
return _vec[0] == v._vec[0] && _vec[1] == v._vec[1] &&
|
||||
_vec[2] == v._vec[2];
|
||||
}
|
||||
|
||||
/// Component-wise inequality
|
||||
bool operator !=(const GfSize3 &v) const {
|
||||
return ! (*this == v);
|
||||
}
|
||||
|
||||
/// Component-wise in-place addition
|
||||
GfSize3 & operator +=(const GfSize3 &v) {
|
||||
_vec[0] += v._vec[0];
|
||||
_vec[1] += v._vec[1];
|
||||
_vec[2] += v._vec[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place subtraction
|
||||
GfSize3 & operator -=(const GfSize3 &v) {
|
||||
_vec[0] -= v._vec[0];
|
||||
_vec[1] -= v._vec[1];
|
||||
_vec[2] -= v._vec[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place multiplication.
|
||||
GfSize3 & operator *=(GfSize3 const &v) {
|
||||
_vec[0] *= v._vec[0];
|
||||
_vec[1] *= v._vec[1];
|
||||
_vec[2] *= v._vec[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place multiplication by a scalar
|
||||
GfSize3 & operator *=(size_t d) {
|
||||
_vec[0] = _vec[0] * d;
|
||||
_vec[1] = _vec[1] * d;
|
||||
_vec[2] = _vec[2] * d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise in-place division by a scalar
|
||||
GfSize3 & operator /=(size_t d) {
|
||||
_vec[0] = _vec[0] / d;
|
||||
_vec[1] = _vec[1] / d;
|
||||
_vec[2] = _vec[2] / d;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Component-wise addition
|
||||
friend GfSize3 operator +(const GfSize3 &v1, const GfSize3 &v3) {
|
||||
return GfSize3(v1._vec[0]+v3._vec[0],
|
||||
v1._vec[1]+v3._vec[1],
|
||||
v1._vec[2]+v3._vec[2]);
|
||||
}
|
||||
|
||||
/// Component-wise subtraction
|
||||
friend GfSize3 operator -(const GfSize3 &v1, const GfSize3 &v3) {
|
||||
return GfSize3(v1._vec[0]-v3._vec[0],
|
||||
v1._vec[1]-v3._vec[1],
|
||||
v1._vec[2]-v3._vec[2]);
|
||||
}
|
||||
|
||||
/// Component-wise multiplication
|
||||
friend GfSize3 operator *(const GfSize3 &v1, const GfSize3 &v3) {
|
||||
return GfSize3(v1._vec[0]*v3._vec[0],
|
||||
v1._vec[1]*v3._vec[1],
|
||||
v1._vec[2]*v3._vec[2]);
|
||||
}
|
||||
|
||||
/// Component-wise multiplication by a scalar
|
||||
friend GfSize3 operator *(const GfSize3 &v1, size_t s) {
|
||||
return GfSize3(v1._vec[0]*s,
|
||||
v1._vec[1]*s,
|
||||
v1._vec[2]*s);
|
||||
}
|
||||
|
||||
/// Component-wise multiplication by a scalar
|
||||
friend GfSize3 operator *(size_t s, const GfSize3 &v1) {
|
||||
return GfSize3(v1._vec[0]*s,
|
||||
v1._vec[1]*s,
|
||||
v1._vec[2]*s);
|
||||
}
|
||||
|
||||
/// Component-wise division by a scalar
|
||||
friend GfSize3 operator /(const GfSize3 &v1, size_t s) {
|
||||
return GfSize3(v1._vec[0]/s,
|
||||
v1._vec[1]/s,
|
||||
v1._vec[2]/s);
|
||||
}
|
||||
|
||||
/// Output operator
|
||||
friend GF_API std::ostream &operator<<(std::ostream &o, GfSize3 const &v);
|
||||
|
||||
/// Conversion to GfVec3i
|
||||
operator GfVec3i() const {
|
||||
return GfVec3i(_vec[0],_vec[1],_vec[2]);
|
||||
}
|
||||
private:
|
||||
size_t _vec[3];
|
||||
};
|
||||
|
||||
// Friend functions must be declared
|
||||
GF_API std::ostream &operator<<(std::ostream &o, GfSize3 const &v);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_SIZE3_H
|
||||
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_TRAITS_H
|
||||
#define PXR_BASE_GF_TRAITS_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// A metafunction with a static const bool member 'value' that is true for
|
||||
/// GfVec types, like GfVec2i, GfVec4d, etc and false for all other types.
|
||||
template <class T>
|
||||
struct GfIsGfVec { static const bool value = false; };
|
||||
|
||||
/// A metafunction with a static const bool member 'value' that is true for
|
||||
/// GfMatrix types, like GfMatrix3d, GfMatrix4f, etc and false for all other
|
||||
/// types.
|
||||
template <class T>
|
||||
struct GfIsGfMatrix { static const bool value = false; };
|
||||
|
||||
/// A metafunction with a static const bool member 'value' that is true for
|
||||
/// GfQuat types and false for all other types.
|
||||
template <class T>
|
||||
struct GfIsGfQuat { static const bool value = false; };
|
||||
|
||||
/// A metafunction with a static const bool member 'value' that is true for
|
||||
/// GfRange types and false for all other types.
|
||||
template <class T>
|
||||
struct GfIsGfRange { static const bool value = false; };
|
||||
|
||||
/// A metafunction which is equivalent to std::is_floating_point but
|
||||
/// allows for additional specialization for types like GfHalf
|
||||
template <class T>
|
||||
struct GfIsFloatingPoint : public std::is_floating_point<T>{};
|
||||
|
||||
/// A metafunction which is equivalent to std::arithmetic but
|
||||
/// also includes any specializations from GfIsFloatingPoint (like GfHalf)
|
||||
template <class T>
|
||||
struct GfIsArithmetic : public std::integral_constant<
|
||||
bool, GfIsFloatingPoint<T>::value || std::is_arithmetic<T>::value>{};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_TRAITS_H
|
||||
@@ -0,0 +1,252 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_GF_TRANSFORM_H
|
||||
#define PXR_BASE_GF_TRANSFORM_H
|
||||
|
||||
/// \file gf/transform.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/gf/rotation.h"
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfMatrix4d;
|
||||
|
||||
/// \class GfTransform
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type: Compound linear transformation.
|
||||
///
|
||||
/// This class represents a linear transformation specified as a series of
|
||||
/// individual components: a \em translation, a \em rotation, a \em scale, a
|
||||
/// \em pivotPosition, and a \em pivotOrientation. When applied to a point,
|
||||
/// the point will be transformed as follows (in order):
|
||||
///
|
||||
/// \li Scaled by the \em scale with respect to \em pivotPosition and the
|
||||
/// orientation specified by the \em pivotOrientation.
|
||||
/// \li Rotated by the \em rotation about \em pivotPosition.
|
||||
/// \li Translated by \em Translation
|
||||
///
|
||||
/// That is, the cumulative matrix that this represents looks like this.
|
||||
///
|
||||
/// \code
|
||||
/// M = -P * -O * S * O * R * P * T
|
||||
/// \endcode
|
||||
///
|
||||
/// where
|
||||
/// \li \em T is the \em translation matrix
|
||||
/// \li \em P is the matrix that translates by \em pivotPosition
|
||||
/// \li \em R is the \em rotation matrix
|
||||
/// \li \em O is the matrix that rotates to \em pivotOrientation
|
||||
/// \li \em S is the \em scale matrix
|
||||
///
|
||||
class GfTransform {
|
||||
|
||||
public:
|
||||
|
||||
/// The default constructor sets the component values to the
|
||||
/// identity transformation.
|
||||
GfTransform() {
|
||||
SetIdentity();
|
||||
}
|
||||
|
||||
/// This constructor initializes the transformation from all
|
||||
/// component values. This is the constructor used by 2x code.
|
||||
GfTransform(const GfVec3d &scale,
|
||||
const GfRotation &pivotOrientation,
|
||||
const GfRotation &rotation,
|
||||
const GfVec3d &pivotPosition,
|
||||
const GfVec3d &translation) {
|
||||
Set(scale, pivotOrientation, rotation, pivotPosition, translation);
|
||||
}
|
||||
|
||||
/// This constructor initializes the transformation from all
|
||||
/// component values. This is the constructor used by 3x code.
|
||||
GfTransform(const GfVec3d &translation,
|
||||
const GfRotation &rotation,
|
||||
const GfVec3d &scale,
|
||||
const GfVec3d &pivotPosition,
|
||||
const GfRotation &pivotOrientation) {
|
||||
Set(translation, rotation, scale, pivotPosition, pivotOrientation);
|
||||
}
|
||||
|
||||
/// This constructor initializes the transformation with a matrix. See
|
||||
/// SetMatrix() for more information.
|
||||
GfTransform(const GfMatrix4d &m) {
|
||||
SetIdentity();
|
||||
SetMatrix(m);
|
||||
}
|
||||
|
||||
/// Sets the transformation from all component values.
|
||||
/// This constructor orders its arguments the way that 2x expects.
|
||||
GF_API
|
||||
GfTransform & Set(const GfVec3d &scale,
|
||||
const GfRotation &pivotOrientation,
|
||||
const GfRotation &rotation,
|
||||
const GfVec3d &pivotPosition,
|
||||
const GfVec3d &translation);
|
||||
|
||||
/// Sets the transformation from all component values.
|
||||
/// This constructor orders its arguments the way that 3x expects.
|
||||
GfTransform & Set(const GfVec3d &translation,
|
||||
const GfRotation &rotation,
|
||||
const GfVec3d &scale,
|
||||
const GfVec3d &pivotPosition,
|
||||
const GfRotation &pivotOrientation) {
|
||||
return Set(scale, pivotOrientation, rotation,
|
||||
pivotPosition, translation);
|
||||
}
|
||||
|
||||
/// Sets the transform components to implement the transformation
|
||||
/// represented by matrix \p m , ignoring any projection. This tries to
|
||||
/// leave the current center unchanged.
|
||||
GF_API
|
||||
GfTransform & SetMatrix(const GfMatrix4d &m);
|
||||
|
||||
/// Sets the transformation to the identity transformation.
|
||||
GF_API
|
||||
GfTransform & SetIdentity();
|
||||
|
||||
/// Sets the scale component, leaving all others untouched.
|
||||
void SetScale(const GfVec3d &scale) {
|
||||
_scale = scale;
|
||||
}
|
||||
|
||||
/// Sets the pivot orientation component, leaving all others untouched.
|
||||
void SetPivotOrientation(const GfRotation &pivotOrient) {
|
||||
_pivotOrientation = pivotOrient;
|
||||
}
|
||||
|
||||
/// Sets the pivot orientation component, leaving all others untouched.
|
||||
void SetScaleOrientation(const GfRotation &pivotOrient) {
|
||||
SetPivotOrientation(pivotOrient);
|
||||
}
|
||||
|
||||
/// Sets the rotation component, leaving all others untouched.
|
||||
void SetRotation(const GfRotation &rotation) {
|
||||
_rotation = rotation;
|
||||
}
|
||||
|
||||
/// Sets the pivot position component, leaving all others untouched.
|
||||
void SetPivotPosition(const GfVec3d &pivPos) {
|
||||
_pivotPosition = pivPos;
|
||||
}
|
||||
|
||||
/// Sets the pivot position component, leaving all others untouched.
|
||||
void SetCenter(const GfVec3d &pivPos) {
|
||||
SetPivotPosition(pivPos);
|
||||
}
|
||||
|
||||
/// Sets the translation component, leaving all others untouched.
|
||||
void SetTranslation(const GfVec3d &translation) {
|
||||
_translation = translation;
|
||||
}
|
||||
|
||||
/// Returns the scale component.
|
||||
const GfVec3d & GetScale() const {
|
||||
return _scale;
|
||||
}
|
||||
|
||||
/// Returns the pivot orientation component.
|
||||
const GfRotation & GetPivotOrientation() const {
|
||||
return _pivotOrientation;
|
||||
}
|
||||
|
||||
/// Returns the scale orientation component.
|
||||
const GfRotation & GetScaleOrientation() const {
|
||||
return GetPivotOrientation();
|
||||
}
|
||||
|
||||
/// Returns the rotation component.
|
||||
const GfRotation & GetRotation() const {
|
||||
return _rotation;
|
||||
}
|
||||
|
||||
/// Returns the pivot position component.
|
||||
const GfVec3d & GetPivotPosition() const {
|
||||
return _pivotPosition;
|
||||
}
|
||||
|
||||
/// Returns the pivot position component.
|
||||
const GfVec3d & GetCenter() const {
|
||||
return GetPivotPosition();
|
||||
}
|
||||
|
||||
/// Returns the translation component.
|
||||
const GfVec3d & GetTranslation() const {
|
||||
return _translation;
|
||||
}
|
||||
|
||||
/// Returns a \c GfMatrix4d that implements the cumulative transformation.
|
||||
GF_API
|
||||
GfMatrix4d GetMatrix() const;
|
||||
|
||||
/// Component-wise transform equality test. All components must match
|
||||
/// exactly for transforms to be considered equal.
|
||||
GF_API
|
||||
bool operator ==(const GfTransform &xf) const;
|
||||
|
||||
/// Component-wise transform inequality test. All components must match
|
||||
/// exactly for transforms to be considered equal.
|
||||
bool operator !=(const GfTransform &xf) const {
|
||||
return ! (*this == xf);
|
||||
}
|
||||
|
||||
/// Post-multiplies transform \p xf into this transform.
|
||||
GF_API
|
||||
GfTransform & operator *=(const GfTransform &xf);
|
||||
|
||||
/// Returns the product of transforms \p xf1 and \p xf2.
|
||||
friend GfTransform operator *(const GfTransform &xf1,
|
||||
const GfTransform &xf2) {
|
||||
GfTransform xf = xf1;
|
||||
return xf *= xf2;
|
||||
}
|
||||
|
||||
private:
|
||||
/// translation
|
||||
GfVec3d _translation;
|
||||
/// rotation
|
||||
GfRotation _rotation;
|
||||
/// scale factors
|
||||
GfVec3d _scale;
|
||||
/// orientation used for scaling and rotation
|
||||
GfRotation _pivotOrientation;
|
||||
/// center of rotation and scaling
|
||||
GfVec3d _pivotPosition;
|
||||
};
|
||||
|
||||
/// Output a GfTransform using the format
|
||||
/// [scale, scaleorientation, rotation, center, translation].
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream&, const GfTransform&);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_TRANSFORM_H
|
||||
@@ -0,0 +1,397 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC2D_H
|
||||
#define PXR_BASE_GF_VEC2D_H
|
||||
|
||||
/// \file gf/vec2d.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec2d;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec2d> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec2d
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 2 double components.
|
||||
///
|
||||
/// Represents a vector of 2 components of type \c double.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec2d
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef double ScalarType;
|
||||
static const size_t dimension = 2;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec2d() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec2d(double value)
|
||||
: _data{ value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec2d(double s0, double s1)
|
||||
: _data{ s0, s1 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec2d(Scl const *p)
|
||||
: _data{ p[0], p[1] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Implicitly convert from GfVec2f.
|
||||
GfVec2d(class GfVec2f const &other);
|
||||
|
||||
/// Implicitly convert from GfVec2h.
|
||||
GfVec2d(class GfVec2h const &other);
|
||||
|
||||
/// Implicitly convert from GfVec2i.
|
||||
GfVec2d(class GfVec2i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec2d XAxis() {
|
||||
GfVec2d result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec2d YAxis() {
|
||||
GfVec2d result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 2.
|
||||
static GfVec2d Axis(size_t i) {
|
||||
GfVec2d result(0);
|
||||
if (i < 2)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec2d &Set(double s0, double s1) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec2d &Set(double const *a) {
|
||||
return Set(a[0], a[1]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
double const *data() const { return _data; }
|
||||
double *data() { return _data; }
|
||||
double const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
double const &operator[](size_t i) const { return _data[i]; }
|
||||
double &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec2d const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec2d const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1];
|
||||
}
|
||||
bool operator!=(GfVec2d const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2h const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec2d operator-() const {
|
||||
return GfVec2d(-_data[0], -_data[1]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec2d &operator+=(GfVec2d const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2d operator+(GfVec2d const &l, GfVec2d const &r) {
|
||||
return GfVec2d(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec2d &operator-=(GfVec2d const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2d operator-(GfVec2d const &l, GfVec2d const &r) {
|
||||
return GfVec2d(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec2d &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec2d operator*(double s) const {
|
||||
return GfVec2d(*this) *= s;
|
||||
}
|
||||
friend GfVec2d operator*(double s, GfVec2d const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec2d &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec2d operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
double operator*(GfVec2d const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec2d GetProjection(GfVec2d const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec2d GetComplement(GfVec2d const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
double GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
double GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
double length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec2d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfVec2d normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
double _data[2];
|
||||
};
|
||||
|
||||
/// Output a GfVec2d.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec2d const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec2f.h"
|
||||
#include "pxr/base/gf/vec2h.h"
|
||||
#include "pxr/base/gf/vec2i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec2d::GfVec2d(class GfVec2f const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
inline
|
||||
GfVec2d::GfVec2d(class GfVec2h const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
inline
|
||||
GfVec2d::GfVec2d(class GfVec2i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec2d
|
||||
GfCompMult(GfVec2d const &v1, GfVec2d const &v2) {
|
||||
return GfVec2d(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec2d
|
||||
GfCompDiv(GfVec2d const &v1, GfVec2d const &v2) {
|
||||
return GfVec2d(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline double
|
||||
GfDot(GfVec2d const &v1, GfVec2d const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline double
|
||||
GfGetLength(GfVec2d const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline double
|
||||
GfNormalize(GfVec2d *v, double eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec2d
|
||||
GfGetNormalized(GfVec2d const &v, double eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec2d
|
||||
GfGetProjection(GfVec2d const &a, GfVec2d const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec2d
|
||||
GfGetComplement(GfVec2d const &a, GfVec2d const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec2d const &v1, GfVec2d const &v2, double tolerance)
|
||||
{
|
||||
GfVec2d delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC2D_H
|
||||
@@ -0,0 +1,397 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC2F_H
|
||||
#define PXR_BASE_GF_VEC2F_H
|
||||
|
||||
/// \file gf/vec2f.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec2f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec2f> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec2f
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 2 float components.
|
||||
///
|
||||
/// Represents a vector of 2 components of type \c float.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec2f
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef float ScalarType;
|
||||
static const size_t dimension = 2;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec2f() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec2f(float value)
|
||||
: _data{ value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec2f(float s0, float s1)
|
||||
: _data{ s0, s1 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec2f(Scl const *p)
|
||||
: _data{ p[0], p[1] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfVec2d.
|
||||
explicit GfVec2f(class GfVec2d const &other);
|
||||
|
||||
/// Implicitly convert from GfVec2h.
|
||||
GfVec2f(class GfVec2h const &other);
|
||||
|
||||
/// Implicitly convert from GfVec2i.
|
||||
GfVec2f(class GfVec2i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec2f XAxis() {
|
||||
GfVec2f result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec2f YAxis() {
|
||||
GfVec2f result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 2.
|
||||
static GfVec2f Axis(size_t i) {
|
||||
GfVec2f result(0);
|
||||
if (i < 2)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec2f &Set(float s0, float s1) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec2f &Set(float const *a) {
|
||||
return Set(a[0], a[1]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
float const *data() const { return _data; }
|
||||
float *data() { return _data; }
|
||||
float const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
float const &operator[](size_t i) const { return _data[i]; }
|
||||
float &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec2f const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec2f const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1];
|
||||
}
|
||||
bool operator!=(GfVec2f const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2h const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec2f operator-() const {
|
||||
return GfVec2f(-_data[0], -_data[1]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec2f &operator+=(GfVec2f const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2f operator+(GfVec2f const &l, GfVec2f const &r) {
|
||||
return GfVec2f(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec2f &operator-=(GfVec2f const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2f operator-(GfVec2f const &l, GfVec2f const &r) {
|
||||
return GfVec2f(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec2f &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec2f operator*(double s) const {
|
||||
return GfVec2f(*this) *= s;
|
||||
}
|
||||
friend GfVec2f operator*(double s, GfVec2f const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec2f &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec2f operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
float operator*(GfVec2f const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec2f GetProjection(GfVec2f const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec2f GetComplement(GfVec2f const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
float GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
float GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
float length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec2f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfVec2f normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
float _data[2];
|
||||
};
|
||||
|
||||
/// Output a GfVec2f.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec2f const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/vec2h.h"
|
||||
#include "pxr/base/gf/vec2i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec2f::GfVec2f(class GfVec2d const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
inline
|
||||
GfVec2f::GfVec2f(class GfVec2h const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
inline
|
||||
GfVec2f::GfVec2f(class GfVec2i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec2f
|
||||
GfCompMult(GfVec2f const &v1, GfVec2f const &v2) {
|
||||
return GfVec2f(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec2f
|
||||
GfCompDiv(GfVec2f const &v1, GfVec2f const &v2) {
|
||||
return GfVec2f(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline float
|
||||
GfDot(GfVec2f const &v1, GfVec2f const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline float
|
||||
GfGetLength(GfVec2f const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline float
|
||||
GfNormalize(GfVec2f *v, float eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec2f
|
||||
GfGetNormalized(GfVec2f const &v, float eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec2f
|
||||
GfGetProjection(GfVec2f const &a, GfVec2f const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec2f
|
||||
GfGetComplement(GfVec2f const &a, GfVec2f const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec2f const &v1, GfVec2f const &v2, double tolerance)
|
||||
{
|
||||
GfVec2f delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC2F_H
|
||||
@@ -0,0 +1,398 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC2H_H
|
||||
#define PXR_BASE_GF_VEC2H_H
|
||||
|
||||
/// \file gf/vec2h.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/half.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec2h;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec2h> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec2h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 2 GfHalf components.
|
||||
///
|
||||
/// Represents a vector of 2 components of type \c GfHalf.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec2h
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef GfHalf ScalarType;
|
||||
static const size_t dimension = 2;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec2h() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec2h(GfHalf value)
|
||||
: _data{ value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec2h(GfHalf s0, GfHalf s1)
|
||||
: _data{ s0, s1 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec2h(Scl const *p)
|
||||
: _data{ p[0], p[1] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfVec2d.
|
||||
explicit GfVec2h(class GfVec2d const &other);
|
||||
|
||||
/// Construct from GfVec2f.
|
||||
explicit GfVec2h(class GfVec2f const &other);
|
||||
|
||||
/// Implicitly convert from GfVec2i.
|
||||
GfVec2h(class GfVec2i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec2h XAxis() {
|
||||
GfVec2h result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec2h YAxis() {
|
||||
GfVec2h result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 2.
|
||||
static GfVec2h Axis(size_t i) {
|
||||
GfVec2h result(0);
|
||||
if (i < 2)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec2h &Set(GfHalf s0, GfHalf s1) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec2h &Set(GfHalf const *a) {
|
||||
return Set(a[0], a[1]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
GfHalf const *data() const { return _data; }
|
||||
GfHalf *data() { return _data; }
|
||||
GfHalf const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
GfHalf const &operator[](size_t i) const { return _data[i]; }
|
||||
GfHalf &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec2h const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec2h const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1];
|
||||
}
|
||||
bool operator!=(GfVec2h const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec2h operator-() const {
|
||||
return GfVec2h(-_data[0], -_data[1]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec2h &operator+=(GfVec2h const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2h operator+(GfVec2h const &l, GfVec2h const &r) {
|
||||
return GfVec2h(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec2h &operator-=(GfVec2h const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2h operator-(GfVec2h const &l, GfVec2h const &r) {
|
||||
return GfVec2h(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec2h &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec2h operator*(double s) const {
|
||||
return GfVec2h(*this) *= s;
|
||||
}
|
||||
friend GfVec2h operator*(double s, GfVec2h const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec2h &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec2h operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
GfHalf operator*(GfVec2h const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec2h GetProjection(GfVec2h const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec2h GetComplement(GfVec2h const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
GfHalf GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
GfHalf GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
GfHalf Normalize(GfHalf eps = 0.001) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
GfHalf length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec2h GetNormalized(GfHalf eps = 0.001) const {
|
||||
GfVec2h normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
GfHalf _data[2];
|
||||
};
|
||||
|
||||
/// Output a GfVec2h.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec2h const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec2d.h"
|
||||
#include "pxr/base/gf/vec2f.h"
|
||||
#include "pxr/base/gf/vec2i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec2h::GfVec2h(class GfVec2d const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
inline
|
||||
GfVec2h::GfVec2h(class GfVec2f const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
inline
|
||||
GfVec2h::GfVec2h(class GfVec2i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec2h
|
||||
GfCompMult(GfVec2h const &v1, GfVec2h const &v2) {
|
||||
return GfVec2h(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec2h
|
||||
GfCompDiv(GfVec2h const &v1, GfVec2h const &v2) {
|
||||
return GfVec2h(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline GfHalf
|
||||
GfDot(GfVec2h const &v1, GfVec2h const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline GfHalf
|
||||
GfGetLength(GfVec2h const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline GfHalf
|
||||
GfNormalize(GfVec2h *v, GfHalf eps = 0.001)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec2h
|
||||
GfGetNormalized(GfVec2h const &v, GfHalf eps = 0.001)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec2h
|
||||
GfGetProjection(GfVec2h const &a, GfVec2h const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec2h
|
||||
GfGetComplement(GfVec2h const &a, GfVec2h const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec2h const &v1, GfVec2h const &v2, double tolerance)
|
||||
{
|
||||
GfVec2h delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC2H_H
|
||||
@@ -0,0 +1,273 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC2I_H
|
||||
#define PXR_BASE_GF_VEC2I_H
|
||||
|
||||
/// \file gf/vec2i.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec2i;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec2i> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec2i
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 2 int components.
|
||||
///
|
||||
/// Represents a vector of 2 components of type \c int.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec2i
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef int ScalarType;
|
||||
static const size_t dimension = 2;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec2i() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec2i(int value)
|
||||
: _data{ value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec2i(int s0, int s1)
|
||||
: _data{ s0, s1 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec2i(Scl const *p)
|
||||
: _data{ p[0], p[1] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec2i XAxis() {
|
||||
GfVec2i result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec2i YAxis() {
|
||||
GfVec2i result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 2.
|
||||
static GfVec2i Axis(size_t i) {
|
||||
GfVec2i result(0);
|
||||
if (i < 2)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec2i &Set(int s0, int s1) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec2i &Set(int const *a) {
|
||||
return Set(a[0], a[1]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
int const *data() const { return _data; }
|
||||
int *data() { return _data; }
|
||||
int const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
int const &operator[](size_t i) const { return _data[i]; }
|
||||
int &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec2i const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec2i const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1];
|
||||
}
|
||||
bool operator!=(GfVec2i const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec2h const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec2i operator-() const {
|
||||
return GfVec2i(-_data[0], -_data[1]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec2i &operator+=(GfVec2i const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2i operator+(GfVec2i const &l, GfVec2i const &r) {
|
||||
return GfVec2i(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec2i &operator-=(GfVec2i const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec2i operator-(GfVec2i const &l, GfVec2i const &r) {
|
||||
return GfVec2i(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec2i &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec2i operator*(double s) const {
|
||||
return GfVec2i(*this) *= s;
|
||||
}
|
||||
friend GfVec2i operator*(double s, GfVec2i const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
GfVec2i &operator/=(int s) {
|
||||
_data[0] /= s;
|
||||
_data[1] /= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec2i operator/(int s) const {
|
||||
return GfVec2i(*this) /= s;
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
int operator*(GfVec2i const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec2i GetProjection(GfVec2i const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec2i GetComplement(GfVec2i const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
int GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int _data[2];
|
||||
};
|
||||
|
||||
/// Output a GfVec2i.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec2i const &);
|
||||
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec2i
|
||||
GfCompMult(GfVec2i const &v1, GfVec2i const &v2) {
|
||||
return GfVec2i(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec2i
|
||||
GfCompDiv(GfVec2i const &v1, GfVec2i const &v2) {
|
||||
return GfVec2i(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline int
|
||||
GfDot(GfVec2i const &v1, GfVec2i const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC2I_H
|
||||
@@ -0,0 +1,470 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC3D_H
|
||||
#define PXR_BASE_GF_VEC3D_H
|
||||
|
||||
/// \file gf/vec3d.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec3d;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec3d> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec3d
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 3 double components.
|
||||
///
|
||||
/// Represents a vector of 3 components of type \c double.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec3d
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef double ScalarType;
|
||||
static const size_t dimension = 3;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec3d() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec3d(double value)
|
||||
: _data{ value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec3d(double s0, double s1, double s2)
|
||||
: _data{ s0, s1, s2 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec3d(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Implicitly convert from GfVec3f.
|
||||
GfVec3d(class GfVec3f const &other);
|
||||
|
||||
/// Implicitly convert from GfVec3h.
|
||||
GfVec3d(class GfVec3h const &other);
|
||||
|
||||
/// Implicitly convert from GfVec3i.
|
||||
GfVec3d(class GfVec3i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec3d XAxis() {
|
||||
GfVec3d result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec3d YAxis() {
|
||||
GfVec3d result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec3d ZAxis() {
|
||||
GfVec3d result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 3.
|
||||
static GfVec3d Axis(size_t i) {
|
||||
GfVec3d result(0);
|
||||
if (i < 3)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec3d &Set(double s0, double s1, double s2) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec3d &Set(double const *a) {
|
||||
return Set(a[0], a[1], a[2]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
double const *data() const { return _data; }
|
||||
double *data() { return _data; }
|
||||
double const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
double const &operator[](size_t i) const { return _data[i]; }
|
||||
double &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec3d const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec3d const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2];
|
||||
}
|
||||
bool operator!=(GfVec3d const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3h const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec3d operator-() const {
|
||||
return GfVec3d(-_data[0], -_data[1], -_data[2]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec3d &operator+=(GfVec3d const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3d operator+(GfVec3d const &l, GfVec3d const &r) {
|
||||
return GfVec3d(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec3d &operator-=(GfVec3d const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3d operator-(GfVec3d const &l, GfVec3d const &r) {
|
||||
return GfVec3d(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec3d &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec3d operator*(double s) const {
|
||||
return GfVec3d(*this) *= s;
|
||||
}
|
||||
friend GfVec3d operator*(double s, GfVec3d const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec3d &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec3d operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
double operator*(GfVec3d const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec3d GetProjection(GfVec3d const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec3d GetComplement(GfVec3d const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
double GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
double GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
double length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec3d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfVec3d normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/// Orthogonalize and optionally normalize a set of basis vectors. This
|
||||
/// uses an iterative method that is very stable even when the vectors are
|
||||
/// far from orthogonal (close to colinear). The number of iterations and
|
||||
/// thus the computation time does increase as the vectors become close to
|
||||
/// colinear, however. Returns a bool specifying whether the solution
|
||||
/// converged after a number of iterations. If it did not converge, the
|
||||
/// returned vectors will be as close as possible to orthogonal within the
|
||||
/// iteration limit. Colinear vectors will be unaltered, and the method
|
||||
/// will return false.
|
||||
GF_API
|
||||
static bool OrthogonalizeBasis(
|
||||
GfVec3d *tx, GfVec3d *ty, GfVec3d *tz,
|
||||
const bool normalize,
|
||||
double eps = GF_MIN_ORTHO_TOLERANCE);
|
||||
|
||||
/// Sets \c v1 and \c v2 to unit vectors such that v1, v2 and *this are
|
||||
/// mutually orthogonal. If the length L of *this is smaller than \c eps,
|
||||
/// then v1 and v2 will have magnitude L/eps. As a result, the function
|
||||
/// delivers a continuous result as *this shrinks in length.
|
||||
GF_API
|
||||
void BuildOrthonormalFrame(GfVec3d *v1, GfVec3d *v2,
|
||||
double eps = GF_MIN_VECTOR_LENGTH) const;
|
||||
|
||||
|
||||
private:
|
||||
double _data[3];
|
||||
};
|
||||
|
||||
/// Output a GfVec3d.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec3d const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/vec3h.h"
|
||||
#include "pxr/base/gf/vec3i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec3d::GfVec3d(class GfVec3f const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
inline
|
||||
GfVec3d::GfVec3d(class GfVec3h const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
inline
|
||||
GfVec3d::GfVec3d(class GfVec3i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec3d
|
||||
GfCompMult(GfVec3d const &v1, GfVec3d const &v2) {
|
||||
return GfVec3d(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec3d
|
||||
GfCompDiv(GfVec3d const &v1, GfVec3d const &v2) {
|
||||
return GfVec3d(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline double
|
||||
GfDot(GfVec3d const &v1, GfVec3d const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline double
|
||||
GfGetLength(GfVec3d const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline double
|
||||
GfNormalize(GfVec3d *v, double eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec3d
|
||||
GfGetNormalized(GfVec3d const &v, double eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec3d
|
||||
GfGetProjection(GfVec3d const &a, GfVec3d const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec3d
|
||||
GfGetComplement(GfVec3d const &a, GfVec3d const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec3d const &v1, GfVec3d const &v2, double tolerance)
|
||||
{
|
||||
GfVec3d delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
GF_API bool
|
||||
GfOrthogonalizeBasis(GfVec3d *tx, GfVec3d *ty, GfVec3d *tz,
|
||||
bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
|
||||
|
||||
GF_API void
|
||||
GfBuildOrthonormalFrame(GfVec3d const &v0,
|
||||
GfVec3d* v1,
|
||||
GfVec3d* v2,
|
||||
double eps = GF_MIN_VECTOR_LENGTH);
|
||||
|
||||
/// Returns the cross product of \p v1 and \p v2.
|
||||
inline GfVec3d
|
||||
GfCross(GfVec3d const &v1, GfVec3d const &v2)
|
||||
{
|
||||
return GfVec3d(
|
||||
v1[1] * v2[2] - v1[2] * v2[1],
|
||||
v1[2] * v2[0] - v1[0] * v2[2],
|
||||
v1[0] * v2[1] - v1[1] * v2[0]);
|
||||
}
|
||||
|
||||
/// Returns the cross product of \p v1 and \p v2.
|
||||
/// \see GfCross()
|
||||
inline GfVec3d
|
||||
operator^(GfVec3d const &v1, GfVec3d const &v2)
|
||||
{
|
||||
return GfCross(v1, v2);
|
||||
}
|
||||
|
||||
/// Spherical linear interpolation in three dimensions.
|
||||
GF_API GfVec3d
|
||||
GfSlerp(double alpha, GfVec3d const &v0, GfVec3d const &v1);
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC3D_H
|
||||
@@ -0,0 +1,470 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC3F_H
|
||||
#define PXR_BASE_GF_VEC3F_H
|
||||
|
||||
/// \file gf/vec3f.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec3f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec3f> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec3f
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 3 float components.
|
||||
///
|
||||
/// Represents a vector of 3 components of type \c float.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec3f
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef float ScalarType;
|
||||
static const size_t dimension = 3;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec3f() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec3f(float value)
|
||||
: _data{ value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec3f(float s0, float s1, float s2)
|
||||
: _data{ s0, s1, s2 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec3f(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfVec3d.
|
||||
explicit GfVec3f(class GfVec3d const &other);
|
||||
|
||||
/// Implicitly convert from GfVec3h.
|
||||
GfVec3f(class GfVec3h const &other);
|
||||
|
||||
/// Implicitly convert from GfVec3i.
|
||||
GfVec3f(class GfVec3i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec3f XAxis() {
|
||||
GfVec3f result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec3f YAxis() {
|
||||
GfVec3f result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec3f ZAxis() {
|
||||
GfVec3f result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 3.
|
||||
static GfVec3f Axis(size_t i) {
|
||||
GfVec3f result(0);
|
||||
if (i < 3)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec3f &Set(float s0, float s1, float s2) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec3f &Set(float const *a) {
|
||||
return Set(a[0], a[1], a[2]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
float const *data() const { return _data; }
|
||||
float *data() { return _data; }
|
||||
float const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
float const &operator[](size_t i) const { return _data[i]; }
|
||||
float &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec3f const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec3f const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2];
|
||||
}
|
||||
bool operator!=(GfVec3f const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3h const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec3f operator-() const {
|
||||
return GfVec3f(-_data[0], -_data[1], -_data[2]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec3f &operator+=(GfVec3f const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3f operator+(GfVec3f const &l, GfVec3f const &r) {
|
||||
return GfVec3f(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec3f &operator-=(GfVec3f const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3f operator-(GfVec3f const &l, GfVec3f const &r) {
|
||||
return GfVec3f(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec3f &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec3f operator*(double s) const {
|
||||
return GfVec3f(*this) *= s;
|
||||
}
|
||||
friend GfVec3f operator*(double s, GfVec3f const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec3f &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec3f operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
float operator*(GfVec3f const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec3f GetProjection(GfVec3f const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec3f GetComplement(GfVec3f const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
float GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
float GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
float length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec3f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfVec3f normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/// Orthogonalize and optionally normalize a set of basis vectors. This
|
||||
/// uses an iterative method that is very stable even when the vectors are
|
||||
/// far from orthogonal (close to colinear). The number of iterations and
|
||||
/// thus the computation time does increase as the vectors become close to
|
||||
/// colinear, however. Returns a bool specifying whether the solution
|
||||
/// converged after a number of iterations. If it did not converge, the
|
||||
/// returned vectors will be as close as possible to orthogonal within the
|
||||
/// iteration limit. Colinear vectors will be unaltered, and the method
|
||||
/// will return false.
|
||||
GF_API
|
||||
static bool OrthogonalizeBasis(
|
||||
GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
|
||||
const bool normalize,
|
||||
double eps = GF_MIN_ORTHO_TOLERANCE);
|
||||
|
||||
/// Sets \c v1 and \c v2 to unit vectors such that v1, v2 and *this are
|
||||
/// mutually orthogonal. If the length L of *this is smaller than \c eps,
|
||||
/// then v1 and v2 will have magnitude L/eps. As a result, the function
|
||||
/// delivers a continuous result as *this shrinks in length.
|
||||
GF_API
|
||||
void BuildOrthonormalFrame(GfVec3f *v1, GfVec3f *v2,
|
||||
float eps = GF_MIN_VECTOR_LENGTH) const;
|
||||
|
||||
|
||||
private:
|
||||
float _data[3];
|
||||
};
|
||||
|
||||
/// Output a GfVec3f.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec3f const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/vec3h.h"
|
||||
#include "pxr/base/gf/vec3i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec3f::GfVec3f(class GfVec3d const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
inline
|
||||
GfVec3f::GfVec3f(class GfVec3h const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
inline
|
||||
GfVec3f::GfVec3f(class GfVec3i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec3f
|
||||
GfCompMult(GfVec3f const &v1, GfVec3f const &v2) {
|
||||
return GfVec3f(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec3f
|
||||
GfCompDiv(GfVec3f const &v1, GfVec3f const &v2) {
|
||||
return GfVec3f(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline float
|
||||
GfDot(GfVec3f const &v1, GfVec3f const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline float
|
||||
GfGetLength(GfVec3f const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline float
|
||||
GfNormalize(GfVec3f *v, float eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec3f
|
||||
GfGetNormalized(GfVec3f const &v, float eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec3f
|
||||
GfGetProjection(GfVec3f const &a, GfVec3f const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec3f
|
||||
GfGetComplement(GfVec3f const &a, GfVec3f const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec3f const &v1, GfVec3f const &v2, double tolerance)
|
||||
{
|
||||
GfVec3f delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
GF_API bool
|
||||
GfOrthogonalizeBasis(GfVec3f *tx, GfVec3f *ty, GfVec3f *tz,
|
||||
bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
|
||||
|
||||
GF_API void
|
||||
GfBuildOrthonormalFrame(GfVec3f const &v0,
|
||||
GfVec3f* v1,
|
||||
GfVec3f* v2,
|
||||
float eps = GF_MIN_VECTOR_LENGTH);
|
||||
|
||||
/// Returns the cross product of \p v1 and \p v2.
|
||||
inline GfVec3f
|
||||
GfCross(GfVec3f const &v1, GfVec3f const &v2)
|
||||
{
|
||||
return GfVec3f(
|
||||
v1[1] * v2[2] - v1[2] * v2[1],
|
||||
v1[2] * v2[0] - v1[0] * v2[2],
|
||||
v1[0] * v2[1] - v1[1] * v2[0]);
|
||||
}
|
||||
|
||||
/// Returns the cross product of \p v1 and \p v2.
|
||||
/// \see GfCross()
|
||||
inline GfVec3f
|
||||
operator^(GfVec3f const &v1, GfVec3f const &v2)
|
||||
{
|
||||
return GfCross(v1, v2);
|
||||
}
|
||||
|
||||
/// Spherical linear interpolation in three dimensions.
|
||||
GF_API GfVec3f
|
||||
GfSlerp(double alpha, GfVec3f const &v0, GfVec3f const &v1);
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC3F_H
|
||||
@@ -0,0 +1,471 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC3H_H
|
||||
#define PXR_BASE_GF_VEC3H_H
|
||||
|
||||
/// \file gf/vec3h.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/half.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec3h;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec3h> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec3h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 3 GfHalf components.
|
||||
///
|
||||
/// Represents a vector of 3 components of type \c GfHalf.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec3h
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef GfHalf ScalarType;
|
||||
static const size_t dimension = 3;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec3h() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec3h(GfHalf value)
|
||||
: _data{ value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec3h(GfHalf s0, GfHalf s1, GfHalf s2)
|
||||
: _data{ s0, s1, s2 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec3h(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfVec3d.
|
||||
explicit GfVec3h(class GfVec3d const &other);
|
||||
|
||||
/// Construct from GfVec3f.
|
||||
explicit GfVec3h(class GfVec3f const &other);
|
||||
|
||||
/// Implicitly convert from GfVec3i.
|
||||
GfVec3h(class GfVec3i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec3h XAxis() {
|
||||
GfVec3h result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec3h YAxis() {
|
||||
GfVec3h result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec3h ZAxis() {
|
||||
GfVec3h result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 3.
|
||||
static GfVec3h Axis(size_t i) {
|
||||
GfVec3h result(0);
|
||||
if (i < 3)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec3h &Set(GfHalf s0, GfHalf s1, GfHalf s2) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec3h &Set(GfHalf const *a) {
|
||||
return Set(a[0], a[1], a[2]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
GfHalf const *data() const { return _data; }
|
||||
GfHalf *data() { return _data; }
|
||||
GfHalf const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
GfHalf const &operator[](size_t i) const { return _data[i]; }
|
||||
GfHalf &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec3h const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec3h const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2];
|
||||
}
|
||||
bool operator!=(GfVec3h const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec3h operator-() const {
|
||||
return GfVec3h(-_data[0], -_data[1], -_data[2]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec3h &operator+=(GfVec3h const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3h operator+(GfVec3h const &l, GfVec3h const &r) {
|
||||
return GfVec3h(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec3h &operator-=(GfVec3h const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3h operator-(GfVec3h const &l, GfVec3h const &r) {
|
||||
return GfVec3h(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec3h &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec3h operator*(double s) const {
|
||||
return GfVec3h(*this) *= s;
|
||||
}
|
||||
friend GfVec3h operator*(double s, GfVec3h const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec3h &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec3h operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
GfHalf operator*(GfVec3h const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec3h GetProjection(GfVec3h const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec3h GetComplement(GfVec3h const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
GfHalf GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
GfHalf GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
GfHalf Normalize(GfHalf eps = 0.001) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
GfHalf length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec3h GetNormalized(GfHalf eps = 0.001) const {
|
||||
GfVec3h normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/// Orthogonalize and optionally normalize a set of basis vectors. This
|
||||
/// uses an iterative method that is very stable even when the vectors are
|
||||
/// far from orthogonal (close to colinear). The number of iterations and
|
||||
/// thus the computation time does increase as the vectors become close to
|
||||
/// colinear, however. Returns a bool specifying whether the solution
|
||||
/// converged after a number of iterations. If it did not converge, the
|
||||
/// returned vectors will be as close as possible to orthogonal within the
|
||||
/// iteration limit. Colinear vectors will be unaltered, and the method
|
||||
/// will return false.
|
||||
GF_API
|
||||
static bool OrthogonalizeBasis(
|
||||
GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
|
||||
const bool normalize,
|
||||
double eps = GF_MIN_ORTHO_TOLERANCE);
|
||||
|
||||
/// Sets \c v1 and \c v2 to unit vectors such that v1, v2 and *this are
|
||||
/// mutually orthogonal. If the length L of *this is smaller than \c eps,
|
||||
/// then v1 and v2 will have magnitude L/eps. As a result, the function
|
||||
/// delivers a continuous result as *this shrinks in length.
|
||||
GF_API
|
||||
void BuildOrthonormalFrame(GfVec3h *v1, GfVec3h *v2,
|
||||
GfHalf eps = 0.001) const;
|
||||
|
||||
|
||||
private:
|
||||
GfHalf _data[3];
|
||||
};
|
||||
|
||||
/// Output a GfVec3h.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec3h const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec3d.h"
|
||||
#include "pxr/base/gf/vec3f.h"
|
||||
#include "pxr/base/gf/vec3i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec3h::GfVec3h(class GfVec3d const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
inline
|
||||
GfVec3h::GfVec3h(class GfVec3f const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
inline
|
||||
GfVec3h::GfVec3h(class GfVec3i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec3h
|
||||
GfCompMult(GfVec3h const &v1, GfVec3h const &v2) {
|
||||
return GfVec3h(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec3h
|
||||
GfCompDiv(GfVec3h const &v1, GfVec3h const &v2) {
|
||||
return GfVec3h(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline GfHalf
|
||||
GfDot(GfVec3h const &v1, GfVec3h const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline GfHalf
|
||||
GfGetLength(GfVec3h const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline GfHalf
|
||||
GfNormalize(GfVec3h *v, GfHalf eps = 0.001)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec3h
|
||||
GfGetNormalized(GfVec3h const &v, GfHalf eps = 0.001)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec3h
|
||||
GfGetProjection(GfVec3h const &a, GfVec3h const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec3h
|
||||
GfGetComplement(GfVec3h const &a, GfVec3h const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec3h const &v1, GfVec3h const &v2, double tolerance)
|
||||
{
|
||||
GfVec3h delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
GF_API bool
|
||||
GfOrthogonalizeBasis(GfVec3h *tx, GfVec3h *ty, GfVec3h *tz,
|
||||
bool normalize, double eps = GF_MIN_ORTHO_TOLERANCE);
|
||||
|
||||
GF_API void
|
||||
GfBuildOrthonormalFrame(GfVec3h const &v0,
|
||||
GfVec3h* v1,
|
||||
GfVec3h* v2,
|
||||
GfHalf eps = 0.001);
|
||||
|
||||
/// Returns the cross product of \p v1 and \p v2.
|
||||
inline GfVec3h
|
||||
GfCross(GfVec3h const &v1, GfVec3h const &v2)
|
||||
{
|
||||
return GfVec3h(
|
||||
v1[1] * v2[2] - v1[2] * v2[1],
|
||||
v1[2] * v2[0] - v1[0] * v2[2],
|
||||
v1[0] * v2[1] - v1[1] * v2[0]);
|
||||
}
|
||||
|
||||
/// Returns the cross product of \p v1 and \p v2.
|
||||
/// \see GfCross()
|
||||
inline GfVec3h
|
||||
operator^(GfVec3h const &v1, GfVec3h const &v2)
|
||||
{
|
||||
return GfCross(v1, v2);
|
||||
}
|
||||
|
||||
/// Spherical linear interpolation in three dimensions.
|
||||
GF_API GfVec3h
|
||||
GfSlerp(double alpha, GfVec3h const &v0, GfVec3h const &v1);
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC3H_H
|
||||
@@ -0,0 +1,288 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC3I_H
|
||||
#define PXR_BASE_GF_VEC3I_H
|
||||
|
||||
/// \file gf/vec3i.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec3i;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec3i> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec3i
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 3 int components.
|
||||
///
|
||||
/// Represents a vector of 3 components of type \c int.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec3i
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef int ScalarType;
|
||||
static const size_t dimension = 3;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec3i() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec3i(int value)
|
||||
: _data{ value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec3i(int s0, int s1, int s2)
|
||||
: _data{ s0, s1, s2 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec3i(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec3i XAxis() {
|
||||
GfVec3i result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec3i YAxis() {
|
||||
GfVec3i result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec3i ZAxis() {
|
||||
GfVec3i result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 3.
|
||||
static GfVec3i Axis(size_t i) {
|
||||
GfVec3i result(0);
|
||||
if (i < 3)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec3i &Set(int s0, int s1, int s2) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec3i &Set(int const *a) {
|
||||
return Set(a[0], a[1], a[2]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
int const *data() const { return _data; }
|
||||
int *data() { return _data; }
|
||||
int const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
int const &operator[](size_t i) const { return _data[i]; }
|
||||
int &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec3i const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec3i const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2];
|
||||
}
|
||||
bool operator!=(GfVec3i const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec3h const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec3i operator-() const {
|
||||
return GfVec3i(-_data[0], -_data[1], -_data[2]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec3i &operator+=(GfVec3i const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3i operator+(GfVec3i const &l, GfVec3i const &r) {
|
||||
return GfVec3i(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec3i &operator-=(GfVec3i const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec3i operator-(GfVec3i const &l, GfVec3i const &r) {
|
||||
return GfVec3i(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec3i &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec3i operator*(double s) const {
|
||||
return GfVec3i(*this) *= s;
|
||||
}
|
||||
friend GfVec3i operator*(double s, GfVec3i const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
GfVec3i &operator/=(int s) {
|
||||
_data[0] /= s;
|
||||
_data[1] /= s;
|
||||
_data[2] /= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec3i operator/(int s) const {
|
||||
return GfVec3i(*this) /= s;
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
int operator*(GfVec3i const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec3i GetProjection(GfVec3i const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec3i GetComplement(GfVec3i const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
int GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int _data[3];
|
||||
};
|
||||
|
||||
/// Output a GfVec3i.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec3i const &);
|
||||
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec3i
|
||||
GfCompMult(GfVec3i const &v1, GfVec3i const &v2) {
|
||||
return GfVec3i(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec3i
|
||||
GfCompDiv(GfVec3i const &v1, GfVec3i const &v2) {
|
||||
return GfVec3i(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline int
|
||||
GfDot(GfVec3i const &v1, GfVec3i const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC3I_H
|
||||
@@ -0,0 +1,431 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC4D_H
|
||||
#define PXR_BASE_GF_VEC4D_H
|
||||
|
||||
/// \file gf/vec4d.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec4d;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec4d> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec4d
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 4 double components.
|
||||
///
|
||||
/// Represents a vector of 4 components of type \c double.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec4d
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef double ScalarType;
|
||||
static const size_t dimension = 4;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec4d() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec4d(double value)
|
||||
: _data{ value, value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec4d(double s0, double s1, double s2, double s3)
|
||||
: _data{ s0, s1, s2, s3 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec4d(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2], p[3] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Implicitly convert from GfVec4f.
|
||||
GfVec4d(class GfVec4f const &other);
|
||||
|
||||
/// Implicitly convert from GfVec4h.
|
||||
GfVec4d(class GfVec4h const &other);
|
||||
|
||||
/// Implicitly convert from GfVec4i.
|
||||
GfVec4d(class GfVec4i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec4d XAxis() {
|
||||
GfVec4d result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec4d YAxis() {
|
||||
GfVec4d result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec4d ZAxis() {
|
||||
GfVec4d result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the W-axis.
|
||||
static GfVec4d WAxis() {
|
||||
GfVec4d result(0);
|
||||
result[3] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 4.
|
||||
static GfVec4d Axis(size_t i) {
|
||||
GfVec4d result(0);
|
||||
if (i < 4)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec4d &Set(double s0, double s1, double s2, double s3) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
_data[3] = s3;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec4d &Set(double const *a) {
|
||||
return Set(a[0], a[1], a[2], a[3]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
double const *data() const { return _data; }
|
||||
double *data() { return _data; }
|
||||
double const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
double const &operator[](size_t i) const { return _data[i]; }
|
||||
double &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec4d const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
boost::hash_combine(h, vec[3]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec4d const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2] &&
|
||||
_data[3] == other[3];
|
||||
}
|
||||
bool operator!=(GfVec4d const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4h const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec4d operator-() const {
|
||||
return GfVec4d(-_data[0], -_data[1], -_data[2], -_data[3]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec4d &operator+=(GfVec4d const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
_data[3] += other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4d operator+(GfVec4d const &l, GfVec4d const &r) {
|
||||
return GfVec4d(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec4d &operator-=(GfVec4d const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
_data[3] -= other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4d operator-(GfVec4d const &l, GfVec4d const &r) {
|
||||
return GfVec4d(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec4d &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
_data[3] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec4d operator*(double s) const {
|
||||
return GfVec4d(*this) *= s;
|
||||
}
|
||||
friend GfVec4d operator*(double s, GfVec4d const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec4d &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec4d operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
double operator*(GfVec4d const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec4d GetProjection(GfVec4d const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec4d GetComplement(GfVec4d const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
double GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
double GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
double Normalize(double eps = GF_MIN_VECTOR_LENGTH) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
double length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec4d GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfVec4d normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
double _data[4];
|
||||
};
|
||||
|
||||
/// Output a GfVec4d.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec4d const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec4f.h"
|
||||
#include "pxr/base/gf/vec4h.h"
|
||||
#include "pxr/base/gf/vec4i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec4d::GfVec4d(class GfVec4f const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
inline
|
||||
GfVec4d::GfVec4d(class GfVec4h const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
inline
|
||||
GfVec4d::GfVec4d(class GfVec4i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec4d
|
||||
GfCompMult(GfVec4d const &v1, GfVec4d const &v2) {
|
||||
return GfVec4d(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2],
|
||||
v1[3] * v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec4d
|
||||
GfCompDiv(GfVec4d const &v1, GfVec4d const &v2) {
|
||||
return GfVec4d(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2],
|
||||
v1[3] / v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline double
|
||||
GfDot(GfVec4d const &v1, GfVec4d const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline double
|
||||
GfGetLength(GfVec4d const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline double
|
||||
GfNormalize(GfVec4d *v, double eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec4d
|
||||
GfGetNormalized(GfVec4d const &v, double eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec4d
|
||||
GfGetProjection(GfVec4d const &a, GfVec4d const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec4d
|
||||
GfGetComplement(GfVec4d const &a, GfVec4d const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec4d const &v1, GfVec4d const &v2, double tolerance)
|
||||
{
|
||||
GfVec4d delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC4D_H
|
||||
@@ -0,0 +1,431 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC4F_H
|
||||
#define PXR_BASE_GF_VEC4F_H
|
||||
|
||||
/// \file gf/vec4f.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec4f;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec4f> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec4f
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 4 float components.
|
||||
///
|
||||
/// Represents a vector of 4 components of type \c float.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec4f
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef float ScalarType;
|
||||
static const size_t dimension = 4;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec4f() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec4f(float value)
|
||||
: _data{ value, value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec4f(float s0, float s1, float s2, float s3)
|
||||
: _data{ s0, s1, s2, s3 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec4f(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2], p[3] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfVec4d.
|
||||
explicit GfVec4f(class GfVec4d const &other);
|
||||
|
||||
/// Implicitly convert from GfVec4h.
|
||||
GfVec4f(class GfVec4h const &other);
|
||||
|
||||
/// Implicitly convert from GfVec4i.
|
||||
GfVec4f(class GfVec4i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec4f XAxis() {
|
||||
GfVec4f result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec4f YAxis() {
|
||||
GfVec4f result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec4f ZAxis() {
|
||||
GfVec4f result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the W-axis.
|
||||
static GfVec4f WAxis() {
|
||||
GfVec4f result(0);
|
||||
result[3] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 4.
|
||||
static GfVec4f Axis(size_t i) {
|
||||
GfVec4f result(0);
|
||||
if (i < 4)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec4f &Set(float s0, float s1, float s2, float s3) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
_data[3] = s3;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec4f &Set(float const *a) {
|
||||
return Set(a[0], a[1], a[2], a[3]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
float const *data() const { return _data; }
|
||||
float *data() { return _data; }
|
||||
float const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
float const &operator[](size_t i) const { return _data[i]; }
|
||||
float &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec4f const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
boost::hash_combine(h, vec[3]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec4f const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2] &&
|
||||
_data[3] == other[3];
|
||||
}
|
||||
bool operator!=(GfVec4f const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4h const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec4f operator-() const {
|
||||
return GfVec4f(-_data[0], -_data[1], -_data[2], -_data[3]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec4f &operator+=(GfVec4f const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
_data[3] += other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4f operator+(GfVec4f const &l, GfVec4f const &r) {
|
||||
return GfVec4f(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec4f &operator-=(GfVec4f const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
_data[3] -= other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4f operator-(GfVec4f const &l, GfVec4f const &r) {
|
||||
return GfVec4f(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec4f &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
_data[3] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec4f operator*(double s) const {
|
||||
return GfVec4f(*this) *= s;
|
||||
}
|
||||
friend GfVec4f operator*(double s, GfVec4f const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec4f &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec4f operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
float operator*(GfVec4f const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec4f GetProjection(GfVec4f const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec4f GetComplement(GfVec4f const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
float GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
float GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
float Normalize(float eps = GF_MIN_VECTOR_LENGTH) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
float length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec4f GetNormalized(float eps = GF_MIN_VECTOR_LENGTH) const {
|
||||
GfVec4f normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
float _data[4];
|
||||
};
|
||||
|
||||
/// Output a GfVec4f.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec4f const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec4d.h"
|
||||
#include "pxr/base/gf/vec4h.h"
|
||||
#include "pxr/base/gf/vec4i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec4f::GfVec4f(class GfVec4d const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
inline
|
||||
GfVec4f::GfVec4f(class GfVec4h const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
inline
|
||||
GfVec4f::GfVec4f(class GfVec4i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec4f
|
||||
GfCompMult(GfVec4f const &v1, GfVec4f const &v2) {
|
||||
return GfVec4f(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2],
|
||||
v1[3] * v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec4f
|
||||
GfCompDiv(GfVec4f const &v1, GfVec4f const &v2) {
|
||||
return GfVec4f(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2],
|
||||
v1[3] / v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline float
|
||||
GfDot(GfVec4f const &v1, GfVec4f const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline float
|
||||
GfGetLength(GfVec4f const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline float
|
||||
GfNormalize(GfVec4f *v, float eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec4f
|
||||
GfGetNormalized(GfVec4f const &v, float eps = GF_MIN_VECTOR_LENGTH)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec4f
|
||||
GfGetProjection(GfVec4f const &a, GfVec4f const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec4f
|
||||
GfGetComplement(GfVec4f const &a, GfVec4f const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec4f const &v1, GfVec4f const &v2, double tolerance)
|
||||
{
|
||||
GfVec4f delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC4F_H
|
||||
@@ -0,0 +1,432 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC4H_H
|
||||
#define PXR_BASE_GF_VEC4H_H
|
||||
|
||||
/// \file gf/vec4h.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
#include "pxr/base/gf/math.h"
|
||||
#include "pxr/base/gf/half.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec4h;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec4h> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec4h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 4 GfHalf components.
|
||||
///
|
||||
/// Represents a vector of 4 components of type \c GfHalf.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec4h
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef GfHalf ScalarType;
|
||||
static const size_t dimension = 4;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec4h() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec4h(GfHalf value)
|
||||
: _data{ value, value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec4h(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3)
|
||||
: _data{ s0, s1, s2, s3 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec4h(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2], p[3] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct from GfVec4d.
|
||||
explicit GfVec4h(class GfVec4d const &other);
|
||||
|
||||
/// Construct from GfVec4f.
|
||||
explicit GfVec4h(class GfVec4f const &other);
|
||||
|
||||
/// Implicitly convert from GfVec4i.
|
||||
GfVec4h(class GfVec4i const &other);
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec4h XAxis() {
|
||||
GfVec4h result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec4h YAxis() {
|
||||
GfVec4h result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec4h ZAxis() {
|
||||
GfVec4h result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the W-axis.
|
||||
static GfVec4h WAxis() {
|
||||
GfVec4h result(0);
|
||||
result[3] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 4.
|
||||
static GfVec4h Axis(size_t i) {
|
||||
GfVec4h result(0);
|
||||
if (i < 4)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec4h &Set(GfHalf s0, GfHalf s1, GfHalf s2, GfHalf s3) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
_data[3] = s3;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec4h &Set(GfHalf const *a) {
|
||||
return Set(a[0], a[1], a[2], a[3]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
GfHalf const *data() const { return _data; }
|
||||
GfHalf *data() { return _data; }
|
||||
GfHalf const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
GfHalf const &operator[](size_t i) const { return _data[i]; }
|
||||
GfHalf &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec4h const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
boost::hash_combine(h, vec[3]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec4h const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2] &&
|
||||
_data[3] == other[3];
|
||||
}
|
||||
bool operator!=(GfVec4h const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4i const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec4h operator-() const {
|
||||
return GfVec4h(-_data[0], -_data[1], -_data[2], -_data[3]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec4h &operator+=(GfVec4h const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
_data[3] += other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4h operator+(GfVec4h const &l, GfVec4h const &r) {
|
||||
return GfVec4h(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec4h &operator-=(GfVec4h const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
_data[3] -= other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4h operator-(GfVec4h const &l, GfVec4h const &r) {
|
||||
return GfVec4h(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec4h &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
_data[3] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec4h operator*(double s) const {
|
||||
return GfVec4h(*this) *= s;
|
||||
}
|
||||
friend GfVec4h operator*(double s, GfVec4h const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
// TODO should divide by the scalar type.
|
||||
GfVec4h &operator/=(double s) {
|
||||
// TODO This should not multiply by 1/s, it should do the division.
|
||||
// Doing the division is more numerically stable when s is close to
|
||||
// zero.
|
||||
return *this *= (1.0 / s);
|
||||
}
|
||||
GfVec4h operator/(double s) const {
|
||||
return *this * (1.0 / s);
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
GfHalf operator*(GfVec4h const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec4h GetProjection(GfVec4h const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec4h GetComplement(GfVec4h const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
GfHalf GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
/// Length
|
||||
GfHalf GetLength() const {
|
||||
// TODO should use GfSqrt.
|
||||
return sqrt(GetLengthSq());
|
||||
}
|
||||
|
||||
/// Normalizes the vector in place to unit length, returning the
|
||||
/// length before normalization. If the length of the vector is
|
||||
/// smaller than \p eps, then the vector is set to vector/\c eps.
|
||||
/// The original length of the vector is returned. See also GfNormalize().
|
||||
///
|
||||
/// \todo This was fixed for bug 67777. This is a gcc64 optimizer bug.
|
||||
/// By tickling the code, it no longer tries to write into
|
||||
/// an illegal memory address (in the code section of memory).
|
||||
GfHalf Normalize(GfHalf eps = 0.001) {
|
||||
// TODO this seems suspect... suggest dividing by length so long as
|
||||
// length is not zero.
|
||||
GfHalf length = GetLength();
|
||||
*this /= (length > eps) ? length : eps;
|
||||
return length;
|
||||
}
|
||||
|
||||
GfVec4h GetNormalized(GfHalf eps = 0.001) const {
|
||||
GfVec4h normalized(*this);
|
||||
normalized.Normalize(eps);
|
||||
return normalized;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
GfHalf _data[4];
|
||||
};
|
||||
|
||||
/// Output a GfVec4h.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec4h const &);
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#include "pxr/base/gf/vec4d.h"
|
||||
#include "pxr/base/gf/vec4f.h"
|
||||
#include "pxr/base/gf/vec4i.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
inline
|
||||
GfVec4h::GfVec4h(class GfVec4d const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
inline
|
||||
GfVec4h::GfVec4h(class GfVec4f const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
inline
|
||||
GfVec4h::GfVec4h(class GfVec4i const &other)
|
||||
{
|
||||
_data[0] = other[0];
|
||||
_data[1] = other[1];
|
||||
_data[2] = other[2];
|
||||
_data[3] = other[3];
|
||||
}
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec4h
|
||||
GfCompMult(GfVec4h const &v1, GfVec4h const &v2) {
|
||||
return GfVec4h(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2],
|
||||
v1[3] * v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec4h
|
||||
GfCompDiv(GfVec4h const &v1, GfVec4h const &v2) {
|
||||
return GfVec4h(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2],
|
||||
v1[3] / v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline GfHalf
|
||||
GfDot(GfVec4h const &v1, GfVec4h const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
/// Returns the geometric length of \c v.
|
||||
inline GfHalf
|
||||
GfGetLength(GfVec4h const &v)
|
||||
{
|
||||
return v.GetLength();
|
||||
}
|
||||
|
||||
/// Normalizes \c *v in place to unit length, returning the length before
|
||||
/// normalization. If the length of \c *v is smaller than \p eps then \c *v is
|
||||
/// set to \c *v/eps. The original length of \c *v is returned.
|
||||
inline GfHalf
|
||||
GfNormalize(GfVec4h *v, GfHalf eps = 0.001)
|
||||
{
|
||||
return v->Normalize(eps);
|
||||
}
|
||||
|
||||
/// Returns a normalized (unit-length) vector with the same direction as \p v.
|
||||
/// If the length of this vector is smaller than \p eps, the vector divided by
|
||||
/// \p eps is returned.
|
||||
inline GfVec4h
|
||||
GfGetNormalized(GfVec4h const &v, GfHalf eps = 0.001)
|
||||
{
|
||||
return v.GetNormalized(eps);
|
||||
}
|
||||
|
||||
/// Returns the projection of \p a onto \p b. That is:
|
||||
/// \code
|
||||
/// b * (a * b)
|
||||
/// \endcode
|
||||
inline GfVec4h
|
||||
GfGetProjection(GfVec4h const &a, GfVec4h const &b)
|
||||
{
|
||||
return a.GetProjection(b);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p a.GetProjection(b). That is:
|
||||
/// \code
|
||||
/// a - a.GetProjection(b)
|
||||
/// \endcode
|
||||
inline GfVec4h
|
||||
GfGetComplement(GfVec4h const &a, GfVec4h const &b)
|
||||
{
|
||||
return a.GetComplement(b);
|
||||
}
|
||||
|
||||
/// Tests for equality within a given tolerance, returning \c true if the
|
||||
/// length of the difference vector is less than or equal to \p tolerance.
|
||||
inline bool
|
||||
GfIsClose(GfVec4h const &v1, GfVec4h const &v2, double tolerance)
|
||||
{
|
||||
GfVec4h delta = v1 - v2;
|
||||
return delta.GetLengthSq() <= tolerance * tolerance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC4H_H
|
||||
@@ -0,0 +1,303 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// This file is generated by a script. Do not edit directly. Edit the
|
||||
// vec.template.h file to make changes.
|
||||
|
||||
#ifndef PXR_BASE_GF_VEC4I_H
|
||||
#define PXR_BASE_GF_VEC4I_H
|
||||
|
||||
/// \file gf/vec4i.h
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
#include "pxr/base/gf/api.h"
|
||||
#include "pxr/base/gf/limits.h"
|
||||
#include "pxr/base/gf/traits.h"
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class GfVec4i;
|
||||
|
||||
template <>
|
||||
struct GfIsGfVec<class GfVec4i> { static const bool value = true; };
|
||||
|
||||
/// \class GfVec4i
|
||||
/// \ingroup group_gf_LinearAlgebra
|
||||
///
|
||||
/// Basic type for a vector of 4 int components.
|
||||
///
|
||||
/// Represents a vector of 4 components of type \c int.
|
||||
/// It is intended to be fast and simple.
|
||||
///
|
||||
class GfVec4i
|
||||
{
|
||||
public:
|
||||
/// Scalar element type and dimension.
|
||||
typedef int ScalarType;
|
||||
static const size_t dimension = 4;
|
||||
|
||||
/// Default constructor does no initialization.
|
||||
GfVec4i() = default;
|
||||
|
||||
/// Initialize all elements to a single value.
|
||||
constexpr explicit GfVec4i(int value)
|
||||
: _data{ value, value, value, value }
|
||||
{
|
||||
}
|
||||
|
||||
/// Initialize all elements with explicit arguments.
|
||||
constexpr GfVec4i(int s0, int s1, int s2, int s3)
|
||||
: _data{ s0, s1, s2, s3 }
|
||||
{
|
||||
}
|
||||
|
||||
/// Construct with pointer to values.
|
||||
template <class Scl>
|
||||
constexpr explicit GfVec4i(Scl const *p)
|
||||
: _data{ p[0], p[1], p[2], p[3] }
|
||||
{
|
||||
}
|
||||
|
||||
/// Create a unit vector along the X-axis.
|
||||
static GfVec4i XAxis() {
|
||||
GfVec4i result(0);
|
||||
result[0] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Y-axis.
|
||||
static GfVec4i YAxis() {
|
||||
GfVec4i result(0);
|
||||
result[1] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the Z-axis.
|
||||
static GfVec4i ZAxis() {
|
||||
GfVec4i result(0);
|
||||
result[2] = 1;
|
||||
return result;
|
||||
}
|
||||
/// Create a unit vector along the W-axis.
|
||||
static GfVec4i WAxis() {
|
||||
GfVec4i result(0);
|
||||
result[3] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Create a unit vector along the i-th axis, zero-based. Return the zero
|
||||
/// vector if \p i is greater than or equal to 4.
|
||||
static GfVec4i Axis(size_t i) {
|
||||
GfVec4i result(0);
|
||||
if (i < 4)
|
||||
result[i] = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Set all elements with passed arguments.
|
||||
GfVec4i &Set(int s0, int s1, int s2, int s3) {
|
||||
_data[0] = s0;
|
||||
_data[1] = s1;
|
||||
_data[2] = s2;
|
||||
_data[3] = s3;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Set all elements with a pointer to data.
|
||||
GfVec4i &Set(int const *a) {
|
||||
return Set(a[0], a[1], a[2], a[3]);
|
||||
}
|
||||
|
||||
/// Direct data access.
|
||||
int const *data() const { return _data; }
|
||||
int *data() { return _data; }
|
||||
int const *GetArray() const { return data(); }
|
||||
|
||||
/// Indexing.
|
||||
int const &operator[](size_t i) const { return _data[i]; }
|
||||
int &operator[](size_t i) { return _data[i]; }
|
||||
|
||||
/// Hash.
|
||||
friend inline size_t hash_value(GfVec4i const &vec) {
|
||||
size_t h = 0;
|
||||
boost::hash_combine(h, vec[0]);
|
||||
boost::hash_combine(h, vec[1]);
|
||||
boost::hash_combine(h, vec[2]);
|
||||
boost::hash_combine(h, vec[3]);
|
||||
return h;
|
||||
}
|
||||
|
||||
/// Equality comparison.
|
||||
bool operator==(GfVec4i const &other) const {
|
||||
return _data[0] == other[0] &&
|
||||
_data[1] == other[1] &&
|
||||
_data[2] == other[2] &&
|
||||
_data[3] == other[3];
|
||||
}
|
||||
bool operator!=(GfVec4i const &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// TODO Add inequality for other vec types...
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4d const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4f const &other) const;
|
||||
/// Equality comparison.
|
||||
GF_API
|
||||
bool operator==(class GfVec4h const &other) const;
|
||||
|
||||
/// Create a vec with negated elements.
|
||||
GfVec4i operator-() const {
|
||||
return GfVec4i(-_data[0], -_data[1], -_data[2], -_data[3]);
|
||||
}
|
||||
|
||||
/// Addition.
|
||||
GfVec4i &operator+=(GfVec4i const &other) {
|
||||
_data[0] += other[0];
|
||||
_data[1] += other[1];
|
||||
_data[2] += other[2];
|
||||
_data[3] += other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4i operator+(GfVec4i const &l, GfVec4i const &r) {
|
||||
return GfVec4i(l) += r;
|
||||
}
|
||||
|
||||
/// Subtraction.
|
||||
GfVec4i &operator-=(GfVec4i const &other) {
|
||||
_data[0] -= other[0];
|
||||
_data[1] -= other[1];
|
||||
_data[2] -= other[2];
|
||||
_data[3] -= other[3];
|
||||
return *this;
|
||||
}
|
||||
friend GfVec4i operator-(GfVec4i const &l, GfVec4i const &r) {
|
||||
return GfVec4i(l) -= r;
|
||||
}
|
||||
|
||||
/// Multiplication by scalar.
|
||||
GfVec4i &operator*=(double s) {
|
||||
_data[0] *= s;
|
||||
_data[1] *= s;
|
||||
_data[2] *= s;
|
||||
_data[3] *= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec4i operator*(double s) const {
|
||||
return GfVec4i(*this) *= s;
|
||||
}
|
||||
friend GfVec4i operator*(double s, GfVec4i const &v) {
|
||||
return v * s;
|
||||
}
|
||||
|
||||
/// Division by scalar.
|
||||
GfVec4i &operator/=(int s) {
|
||||
_data[0] /= s;
|
||||
_data[1] /= s;
|
||||
_data[2] /= s;
|
||||
_data[3] /= s;
|
||||
return *this;
|
||||
}
|
||||
GfVec4i operator/(int s) const {
|
||||
return GfVec4i(*this) /= s;
|
||||
}
|
||||
|
||||
/// See GfDot().
|
||||
int operator*(GfVec4i const &v) const {
|
||||
return _data[0] * v[0] + _data[1] * v[1] + _data[2] * v[2] + _data[3] * v[3];
|
||||
}
|
||||
|
||||
/// Returns the projection of \p this onto \p v. That is:
|
||||
/// \code
|
||||
/// v * (*this * v)
|
||||
/// \endcode
|
||||
GfVec4i GetProjection(GfVec4i const &v) const {
|
||||
return v * (*this * v);
|
||||
}
|
||||
|
||||
/// Returns the orthogonal complement of \p this->GetProjection(b).
|
||||
/// That is:
|
||||
/// \code
|
||||
/// *this - this->GetProjection(b)
|
||||
/// \endcode
|
||||
GfVec4i GetComplement(GfVec4i const &b) const {
|
||||
return *this - this->GetProjection(b);
|
||||
}
|
||||
|
||||
/// Squared length.
|
||||
int GetLengthSq() const {
|
||||
return *this * *this;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
int _data[4];
|
||||
};
|
||||
|
||||
/// Output a GfVec4i.
|
||||
/// \ingroup group_gf_DebuggingOutput
|
||||
GF_API std::ostream& operator<<(std::ostream &, GfVec4i const &);
|
||||
|
||||
|
||||
/// Returns component-wise multiplication of vectors \p v1 and \p v2.
|
||||
inline GfVec4i
|
||||
GfCompMult(GfVec4i const &v1, GfVec4i const &v2) {
|
||||
return GfVec4i(
|
||||
v1[0] * v2[0],
|
||||
v1[1] * v2[1],
|
||||
v1[2] * v2[2],
|
||||
v1[3] * v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns component-wise quotient of vectors \p v1 and \p v2.
|
||||
inline GfVec4i
|
||||
GfCompDiv(GfVec4i const &v1, GfVec4i const &v2) {
|
||||
return GfVec4i(
|
||||
v1[0] / v2[0],
|
||||
v1[1] / v2[1],
|
||||
v1[2] / v2[2],
|
||||
v1[3] / v2[3]
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the dot (inner) product of two vectors.
|
||||
inline int
|
||||
GfDot(GfVec4i const &v1, GfVec4i const &v2) {
|
||||
return v1 * v2;
|
||||
}
|
||||
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_GF_VEC4I_H
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_API_H
|
||||
#define PXR_BASE_JS_API_H
|
||||
|
||||
#include "pxr/base/arch/export.h"
|
||||
|
||||
#if defined(PXR_STATIC)
|
||||
# define JS_API
|
||||
# define JS_API_TEMPLATE_CLASS(...)
|
||||
# define JS_API_TEMPLATE_STRUCT(...)
|
||||
# define JS_LOCAL
|
||||
#else
|
||||
# if defined(JS_EXPORTS)
|
||||
# define JS_API ARCH_EXPORT
|
||||
# define JS_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define JS_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# else
|
||||
# define JS_API ARCH_IMPORT
|
||||
# define JS_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define JS_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# endif
|
||||
# define JS_LOCAL ARCH_HIDDEN
|
||||
#endif
|
||||
|
||||
#endif // PXR_BASE_JS_API_H
|
||||
@@ -0,0 +1,145 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_CONVERTER_H
|
||||
#define PXR_BASE_JS_CONVERTER_H
|
||||
|
||||
/// \file js/converter.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/value.h"
|
||||
#include "pxr/base/tf/diagnostic.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
|
||||
// holding an \c int64_t.
|
||||
template <class ValueType, class MapType, bool UseInt64 = true>
|
||||
struct Js_ValueToInt {
|
||||
static ValueType Apply(const JsValue& value) {
|
||||
return value.IsUInt64() ?
|
||||
ValueType(value.GetUInt64()) : ValueType(value.GetInt64());
|
||||
}
|
||||
};
|
||||
|
||||
// Converts a \c JsValue \p value holding an \c int value to a \c ValueType
|
||||
// holding an \c int.
|
||||
template <class ValueType, class MapType>
|
||||
struct Js_ValueToInt<ValueType, MapType, false>
|
||||
{
|
||||
static ValueType Apply(const JsValue& value) {
|
||||
return ValueType(value.GetInt());
|
||||
}
|
||||
};
|
||||
|
||||
/// \class JsValueTypeConverter
|
||||
///
|
||||
/// A helper class that can convert recursive JsValue structures to
|
||||
/// identical structures using a different container type. The destination
|
||||
/// container type is determined by the \c ValueType template parameter, while
|
||||
/// the type to map objects to is determined by the \c MapType template
|
||||
/// parameter.
|
||||
///
|
||||
/// It is expected that the class \c ValueType is default constructable. A
|
||||
/// default constructed \c ValueType is used to represent JSON null. The value
|
||||
/// type must also support construction from the fundamental bool, string,
|
||||
/// real and integer types supported by JsValue.
|
||||
///
|
||||
/// JsArray values are converted to std::vector<ValueType>, and JsObject
|
||||
/// values are converted to the MapType. MapType must have a value type of \c
|
||||
/// ValueType, and support operator[] assignment.
|
||||
///
|
||||
/// If the \c UseInt64 template parameter is \c true (default), value types
|
||||
/// converted from JsValue::IntType hold uint64_t or int64_t. If the parameter
|
||||
/// is \c false, all IntType values are converted to int. Note that this may
|
||||
/// cause truncation if the JsValue holds values too large to be stored in an
|
||||
/// int on this platform.
|
||||
///
|
||||
template <class ValueType, class MapType, bool UseInt64 = true>
|
||||
class JsValueTypeConverter
|
||||
{
|
||||
typedef std::vector<ValueType> VectorType;
|
||||
public:
|
||||
/// Converts the given \p value recursively to a structure using the value
|
||||
/// and map types specified by the \c ValueType and \c MapType class
|
||||
/// template parameters.
|
||||
static ValueType Convert(const JsValue& value) {
|
||||
return _ToValueType(value);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Converts \p value to \c ValueType.
|
||||
static ValueType _ToValueType(const JsValue& value) {
|
||||
switch (value.GetType()) {
|
||||
case JsValue::ObjectType:
|
||||
return ValueType(_ObjectToMap(value.GetJsObject()));
|
||||
case JsValue::ArrayType:
|
||||
return ValueType(_ArrayToVector(value.GetJsArray()));
|
||||
case JsValue::BoolType:
|
||||
return ValueType(value.GetBool());
|
||||
case JsValue::StringType:
|
||||
return ValueType(value.GetString());
|
||||
case JsValue::RealType:
|
||||
return ValueType(value.GetReal());
|
||||
case JsValue::IntType:
|
||||
return Js_ValueToInt<ValueType, MapType, UseInt64>::Apply(value);
|
||||
case JsValue::NullType:
|
||||
return ValueType();
|
||||
default: {
|
||||
TF_CODING_ERROR("unknown value type");
|
||||
return ValueType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts \p object to \c MapType.
|
||||
static MapType _ObjectToMap(const JsObject& object) {
|
||||
MapType result;
|
||||
for (const auto& p : object) {
|
||||
result[p.first] = _ToValueType(p.second);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Converts \p array to \c VectorType.
|
||||
static VectorType _ArrayToVector(const JsArray& array) {
|
||||
VectorType result;
|
||||
result.reserve(array.size());
|
||||
for (const auto& value : array) {
|
||||
result.push_back(_ToValueType(value));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/// Returns \p value converted recursively to the template and map types given
|
||||
/// by the \c ValueType and \p MapType parameters.
|
||||
/// \see JsValueTypeConverter
|
||||
template <class ValueType, class MapType>
|
||||
ValueType JsConvertToContainerType(const JsValue& value) {
|
||||
return JsValueTypeConverter<ValueType, MapType>::Convert(value);
|
||||
}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_CONVERTER_H
|
||||
@@ -0,0 +1,235 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
|
||||
#ifndef PXR_BASE_JS_JSON_H
|
||||
#define PXR_BASE_JS_JSON_H
|
||||
|
||||
/// \file js/json.h
|
||||
/// Top-level entrypoints for reading and writing JSON.
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/api.h"
|
||||
#include "pxr/base/js/value.h"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// \struct JsParseError
|
||||
///
|
||||
/// A struct containing information about a JSON parsing error.
|
||||
///
|
||||
struct JsParseError {
|
||||
JsParseError() : line(0), column(0) { }
|
||||
unsigned int line;
|
||||
unsigned int column;
|
||||
std::string reason;
|
||||
};
|
||||
|
||||
/// Parse the contents of input stream \p istr and return a JsValue. On
|
||||
/// failure, this returns a null JsValue.
|
||||
JS_API
|
||||
JsValue JsParseStream(std::istream& istr, JsParseError* error = 0);
|
||||
|
||||
/// Parse the contents of the JSON string \p data and return it as a JsValue.
|
||||
/// On failure, this returns a null JsValue.
|
||||
JS_API
|
||||
JsValue JsParseString(const std::string& data, JsParseError* error = 0);
|
||||
|
||||
/// Convert the JsValue \p value to JSON and write the result to output stream
|
||||
/// \p ostr.
|
||||
JS_API
|
||||
void JsWriteToStream(const JsValue& value, std::ostream& ostr);
|
||||
|
||||
/// Convert the JsValue \p value to JSON and return it as a string.
|
||||
JS_API
|
||||
std::string JsWriteToString(const JsValue& value);
|
||||
|
||||
/// \class JsWriter
|
||||
///
|
||||
/// This class provides an interface to writing json values directly to a
|
||||
/// stream. This can be much more efficient than constructing a JsValue instance
|
||||
/// and using JsWriteToStream if the data size is significant.
|
||||
///
|
||||
class JsWriter {
|
||||
public:
|
||||
enum class Style {
|
||||
Compact,
|
||||
Pretty
|
||||
};
|
||||
|
||||
/// Constructor. The lifetime of the /p ostr parameter is assumed to be
|
||||
/// longer than the JsWriter instance.
|
||||
JS_API JsWriter(std::ostream& ostr, Style style = Style::Compact);
|
||||
|
||||
/// Destructor.
|
||||
JS_API ~JsWriter();
|
||||
|
||||
/// Disable copies.
|
||||
JsWriter(const JsWriter&) = delete;
|
||||
JsWriter& operator=(const JsWriter&) = delete;
|
||||
|
||||
/// Write a null value.
|
||||
JS_API bool WriteValue(std::nullptr_t);
|
||||
|
||||
/// Write a boolean value.
|
||||
JS_API bool WriteValue(bool b);
|
||||
|
||||
/// Write an integer value.
|
||||
JS_API bool WriteValue(int i);
|
||||
|
||||
/// Write an unsigned integer value.
|
||||
JS_API bool WriteValue(unsigned u);
|
||||
|
||||
/// Write a 64-bit integer value.
|
||||
JS_API bool WriteValue(int64_t i);
|
||||
|
||||
/// Write a 64-bit unsigned integer value.
|
||||
JS_API bool WriteValue(uint64_t u);
|
||||
|
||||
/// Write a double value.
|
||||
JS_API bool WriteValue(double d);
|
||||
|
||||
/// Write a string value.
|
||||
JS_API bool WriteValue(const std::string& s);
|
||||
|
||||
/// Write a string value.
|
||||
JS_API bool WriteValue(const char* s);
|
||||
|
||||
/// Write a string value.
|
||||
template< size_t N>
|
||||
bool WriteValue(const char(&s)[N]) { return _String(s, N-1); }
|
||||
|
||||
/// Write the start of an object.
|
||||
JS_API bool BeginObject();
|
||||
|
||||
/// Write an object key.
|
||||
JS_API bool WriteKey(const std::string&);
|
||||
|
||||
/// Write an object key.
|
||||
JS_API bool WriteKey(const char*);
|
||||
|
||||
/// Write a string literal object key.
|
||||
template< size_t N>
|
||||
bool WriteKey(const char(&s)[N]) { return _Key(s, N-1); }
|
||||
|
||||
/// Convenience function to write an object key and value.
|
||||
template<class K, class V>
|
||||
void WriteKeyValue(K&& k, V&& v) {
|
||||
_WriteObjectFields(std::forward<K>(k), std::forward<V>(v));
|
||||
}
|
||||
|
||||
/// Write the end of an object.
|
||||
JS_API bool EndObject();
|
||||
|
||||
/// Write the start of an array.
|
||||
JS_API bool BeginArray();
|
||||
|
||||
/// Write the end of an array.
|
||||
JS_API bool EndArray();
|
||||
|
||||
/// Convenience function to write an array of values.
|
||||
template <class Container>
|
||||
void WriteArray(const Container& c) {
|
||||
BeginArray();
|
||||
for (const auto& i : c) {
|
||||
WriteValue(i);
|
||||
}
|
||||
EndArray();
|
||||
}
|
||||
|
||||
/// Convenience function to write an array of values by calling the given
|
||||
/// functor for each item in the container.
|
||||
template <class Container, class ItemWriteFn>
|
||||
void WriteArray(const Container& c, const ItemWriteFn& f) {
|
||||
BeginArray();
|
||||
for (const auto& i : c) {
|
||||
f(*this, i);
|
||||
}
|
||||
EndArray();
|
||||
}
|
||||
|
||||
/// Convenience function to write an array of values given two iterators by
|
||||
/// calling the given functor for each item in the container.
|
||||
template <class Iterator, class ItemWriteFn>
|
||||
void WriteArray(
|
||||
const Iterator& begin, const Iterator& end, const ItemWriteFn& f) {
|
||||
BeginArray();
|
||||
for (Iterator i = begin; i != end; ++i) {
|
||||
f(*this, i);
|
||||
}
|
||||
EndArray();
|
||||
}
|
||||
|
||||
/// Convenience function to write an object given key value pair arguments.
|
||||
/// key arguments must be convertable to strings, value argruments must be
|
||||
/// either a writable type, or a callablable type taking a JsWriter&.
|
||||
template< class ...T>
|
||||
void WriteObject(T&&... f) {
|
||||
static_assert(sizeof...(T) %2 == 0,
|
||||
"Arguments must come in key value pairs");
|
||||
BeginObject();
|
||||
_WriteObjectFields(std::forward<T>(f)...);
|
||||
EndObject();
|
||||
}
|
||||
|
||||
private:
|
||||
// Don't want implicit casts to write functions, its better to get an error.
|
||||
template <class T>
|
||||
bool WriteValue(T) = delete;
|
||||
|
||||
JS_API bool _String(const char* s, size_t len);
|
||||
JS_API bool _Key(const char* s, size_t len);
|
||||
|
||||
template <class KeyType, class T>
|
||||
auto _WriteObjectFields(KeyType&& key, T&& v)
|
||||
-> decltype(WriteValue(std::forward<T>(v)), void()) {
|
||||
WriteKey(std::forward<KeyType>(key));
|
||||
WriteValue(std::forward<T>(v));
|
||||
}
|
||||
|
||||
template <class KeyType, class T>
|
||||
auto _WriteObjectFields(KeyType&& key, T&& v)
|
||||
-> decltype(v(std::declval<JsWriter&>()), void()) {
|
||||
WriteKey(std::forward<KeyType>(key));
|
||||
v(*this);
|
||||
}
|
||||
|
||||
template< class Key0, class T0, class ...T>
|
||||
void _WriteObjectFields(Key0&& key0, T0&& f0, T&&...f){
|
||||
_WriteObjectFields(std::forward<Key0>(key0), std::forward<T0>(f0));
|
||||
_WriteObjectFields(std::forward<T>(f)...);
|
||||
}
|
||||
|
||||
class _Impl;
|
||||
std::unique_ptr<_Impl> _impl;
|
||||
};
|
||||
|
||||
/// Write a json value.
|
||||
JS_API void JsWriteValue(JsWriter* writer, const JsValue& value);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_JSON_H
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_TYPES_H
|
||||
#define PXR_BASE_JS_TYPES_H
|
||||
|
||||
/// \file js/types.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
class JsValue;
|
||||
typedef std::map<std::string, JsValue> JsObject;
|
||||
typedef std::vector<JsValue> JsArray;
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_TYPES_H
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_UTILS_H
|
||||
#define PXR_BASE_JS_UTILS_H
|
||||
|
||||
/// \file js/utils.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/api.h"
|
||||
#include "pxr/base/js/value.h"
|
||||
|
||||
#include <boost/none.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <string>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
typedef boost::optional<JsValue> JsOptionalValue;
|
||||
|
||||
/// Returns the value associated with \p key in the given \p object. If no
|
||||
/// such key exists, and the supplied default is not supplied, this method
|
||||
/// returns an uninitialized optional JsValue. Otherwise, the \p
|
||||
/// defaultValue is returned.
|
||||
JS_API
|
||||
JsOptionalValue JsFindValue(
|
||||
const JsObject& object,
|
||||
const std::string& key,
|
||||
const JsOptionalValue& defaultValue = boost::none);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_UTILS_H
|
||||
@@ -0,0 +1,296 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_JS_VALUE_H
|
||||
#define PXR_BASE_JS_VALUE_H
|
||||
|
||||
/// \file js/value.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/js/api.h"
|
||||
#include "pxr/base/js/types.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
// Value API Version
|
||||
// 1 (or undefined) - Initial version.
|
||||
// 2 - Changed Get{Array,Object} to GetJs{Array,Object}.
|
||||
#define JS_VALUE_API_VERSION 2
|
||||
|
||||
/// \class JsValue
|
||||
///
|
||||
/// A discriminated union type for JSON values. A JsValue may contain one of
|
||||
/// the following types:
|
||||
///
|
||||
/// \li JsObject, a dictionary type
|
||||
/// \li JsArray, a vector type
|
||||
/// \li std::string
|
||||
/// \li bool
|
||||
/// \li int64_t
|
||||
/// \li uint64_t
|
||||
/// \li double
|
||||
/// \li null
|
||||
///
|
||||
class JsValue
|
||||
{
|
||||
public:
|
||||
/// Type held by this JSON value.
|
||||
enum Type {
|
||||
ObjectType,
|
||||
ArrayType,
|
||||
StringType,
|
||||
BoolType,
|
||||
IntType,
|
||||
RealType,
|
||||
NullType
|
||||
};
|
||||
|
||||
/// Constructs a null value.
|
||||
JS_API JsValue();
|
||||
|
||||
/// Constructs a value holding the given object.
|
||||
JS_API JsValue(const JsObject& value);
|
||||
|
||||
/// Constructs a value holding the given object rvalue reference.
|
||||
JS_API JsValue(JsObject&& value);
|
||||
|
||||
/// Constructs a value holding the given array.
|
||||
JS_API JsValue(const JsArray& value);
|
||||
|
||||
/// Constructs a value holding the given array rvalue reference.
|
||||
JS_API JsValue(JsArray&& value);
|
||||
|
||||
/// Constructs a value holding the given char array as a std::string.
|
||||
JS_API explicit JsValue(const char* value);
|
||||
|
||||
/// Constructs a value holding the given std::string.
|
||||
JS_API explicit JsValue(const std::string& value);
|
||||
|
||||
/// Constructs a value holding the given std::string rvalue reference.
|
||||
JS_API explicit JsValue(std::string&& value);
|
||||
|
||||
/// Constructs a value holding a bool.
|
||||
JS_API explicit JsValue(bool value);
|
||||
|
||||
/// Constructs a value holding a signed integer.
|
||||
JS_API explicit JsValue(int value);
|
||||
|
||||
/// Constructs a value holding a 64-bit signed integer.
|
||||
JS_API explicit JsValue(int64_t value);
|
||||
|
||||
/// Constructs a value holding a 64-bit unsigned integer.
|
||||
JS_API explicit JsValue(uint64_t value);
|
||||
|
||||
/// Constructs a value holding a double.
|
||||
JS_API explicit JsValue(double value);
|
||||
|
||||
/// Returns the object held by this value. If this value is not holding an
|
||||
/// object, this method raises a coding error and an empty object is
|
||||
/// returned.
|
||||
JS_API const JsObject& GetJsObject() const;
|
||||
|
||||
/// Returns the array held by this value. If this value is not holding an
|
||||
/// array, this method raises a coding error and an empty array is
|
||||
/// returned.
|
||||
JS_API const JsArray& GetJsArray() const;
|
||||
|
||||
/// Returns the string held by this value. If this value is not holding a
|
||||
/// string, this method raises a coding error and an empty string is
|
||||
/// returned.
|
||||
JS_API const std::string& GetString() const;
|
||||
|
||||
/// Returns the bool held by this value. If this value is not holding a
|
||||
/// bool, this method raises a coding error and false is returned.
|
||||
JS_API bool GetBool() const;
|
||||
|
||||
/// Returns the integer held by this value. If this value is not holding
|
||||
/// an int, this method raises a coding error and zero is returned. If the
|
||||
/// value is holding a 64-bit integer larger than the platform int may
|
||||
/// hold, the value is truncated.
|
||||
JS_API int GetInt() const;
|
||||
|
||||
/// Returns the 64-bit integer held by this value. If this value is not
|
||||
/// holding a 64-bit integer, this method raises a coding error and zero
|
||||
/// is returned.
|
||||
JS_API int64_t GetInt64() const;
|
||||
|
||||
/// Returns the 64-bit unsigned integer held by this value. If this value
|
||||
/// is not holding a 64-bit unsigned integer, this method raises a coding
|
||||
/// error and zero is returned.
|
||||
JS_API uint64_t GetUInt64() const;
|
||||
|
||||
/// Returns the double held by this value. If this value is not holding a
|
||||
/// double, this method raises a coding error and zero is returned.
|
||||
JS_API double GetReal() const;
|
||||
|
||||
/// Returns the value corresponding to the C++ type specified in the
|
||||
/// template parameter if it is holding such a value. Calling this
|
||||
/// function with C++ type T is equivalent to calling the specific Get
|
||||
/// function above that returns a value or reference to a type T.
|
||||
///
|
||||
/// If a value corresponding to the C++ type is not being held, this
|
||||
/// method raises a coding error. See Get functions above for default
|
||||
/// value returned in this case.
|
||||
template <typename T,
|
||||
typename ReturnType = typename std::conditional<
|
||||
std::is_same<T, JsObject>::value ||
|
||||
std::is_same<T, JsArray>::value ||
|
||||
std::is_same<T, std::string>::value,
|
||||
const T&, T>::type>
|
||||
ReturnType Get() const {
|
||||
return _Get(static_cast<T*>(nullptr));
|
||||
}
|
||||
|
||||
/// Returns a vector holding the elements of this value's array that
|
||||
/// correspond to the C++ type specified as the template parameter.
|
||||
/// If this value is not holding an array, an empty vector is returned.
|
||||
/// If any of the array's elements does not correspond to the C++ type,
|
||||
/// it is replaced with the default value used by the Get functions above.
|
||||
/// In both cases, a coding error will be raised.
|
||||
template <typename T>
|
||||
std::vector<T> GetArrayOf() const;
|
||||
|
||||
/// Returns the type of this value.
|
||||
JS_API Type GetType() const;
|
||||
|
||||
/// Returns a display name for the type of this value.
|
||||
JS_API std::string GetTypeName() const;
|
||||
|
||||
/// Returns true if this value is holding an object type.
|
||||
JS_API bool IsObject() const;
|
||||
|
||||
/// Returns true if this value is holding an array type.
|
||||
JS_API bool IsArray() const;
|
||||
|
||||
/// Returns true if this value is holding a string type.
|
||||
JS_API bool IsString() const;
|
||||
|
||||
/// Returns true if this value is holding a boolean type.
|
||||
JS_API bool IsBool() const;
|
||||
|
||||
/// Returns true if this value is holding an integer type.
|
||||
JS_API bool IsInt() const;
|
||||
|
||||
/// Returns true if this value is holding a real type.
|
||||
JS_API bool IsReal() const;
|
||||
|
||||
/// Returns true if this value is holding a 64-bit unsigned integer.
|
||||
JS_API bool IsUInt64() const;
|
||||
|
||||
/// Returns true if this value is holding a type that corresponds
|
||||
/// to the C++ type specified as the template parameter.
|
||||
template <typename T>
|
||||
bool Is() const {
|
||||
return _Is(static_cast<T*>(nullptr));
|
||||
}
|
||||
|
||||
/// Returns true if this value is holding an array whose elements all
|
||||
/// correspond to the C++ type specified as the template parameter.
|
||||
template <typename T>
|
||||
bool IsArrayOf() const;
|
||||
|
||||
/// Returns true if this value is null, false otherwise.
|
||||
JS_API bool IsNull() const;
|
||||
|
||||
/// Evaluates to true if this value is not null.
|
||||
JS_API explicit operator bool() const;
|
||||
|
||||
/// Returns true of both values hold the same type and the underlying held
|
||||
/// values are equal.
|
||||
JS_API bool operator==(const JsValue& other) const;
|
||||
|
||||
/// Returns true if values are of different type, or the underlying held
|
||||
/// values are not equal.
|
||||
JS_API bool operator!=(const JsValue& other) const;
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
struct _InvalidTypeHelper : public std::false_type { };
|
||||
|
||||
template <class T>
|
||||
T _Get(T*) const {
|
||||
static_assert(_InvalidTypeHelper<T>::value,
|
||||
"Invalid type for JsValue");
|
||||
return T();
|
||||
}
|
||||
|
||||
const JsObject& _Get(JsObject*) const { return GetJsObject(); }
|
||||
const JsArray& _Get(JsArray*) const { return GetJsArray(); }
|
||||
const std::string& _Get(std::string*) const { return GetString(); }
|
||||
bool _Get(bool*) const { return GetBool(); }
|
||||
int _Get(int*) const { return GetInt(); }
|
||||
int64_t _Get(int64_t*) const { return GetInt64(); }
|
||||
uint64_t _Get(uint64_t*) const { return GetUInt64(); }
|
||||
double _Get(double*) const { return GetReal(); }
|
||||
|
||||
template <class T>
|
||||
bool _Is(T*) const {
|
||||
static_assert(_InvalidTypeHelper<T>::value,
|
||||
"Invalid type for JsValue");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _Is(JsObject*) const { return IsObject(); }
|
||||
bool _Is(JsArray*) const { return IsArray(); }
|
||||
bool _Is(std::string*) const { return IsString(); }
|
||||
bool _Is(bool*) const { return IsBool(); }
|
||||
bool _Is(int*) const { return IsInt(); }
|
||||
bool _Is(int64_t*) const { return IsInt(); }
|
||||
bool _Is(uint64_t*) const { return IsUInt64(); }
|
||||
bool _Is(double*) const { return IsReal(); }
|
||||
|
||||
struct _Holder;
|
||||
std::shared_ptr<_Holder> _holder;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::vector<T> JsValue::GetArrayOf() const
|
||||
{
|
||||
const JsArray& array = GetJsArray();
|
||||
std::vector<T> result(array.size());
|
||||
std::transform(array.begin(), array.end(), result.begin(),
|
||||
[](const JsValue& v) { return v.Get<T>(); });
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool JsValue::IsArrayOf() const
|
||||
{
|
||||
if (!IsArray()) {
|
||||
return false;
|
||||
}
|
||||
const JsArray& array = GetJsArray();
|
||||
return std::all_of(array.begin(), array.end(),
|
||||
[](const JsValue& v) { return v.Is<T>(); });
|
||||
}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_JS_VALUE_H
|
||||
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_API_H
|
||||
#define PXR_BASE_PLUG_API_H
|
||||
|
||||
#include "pxr/base/arch/export.h"
|
||||
|
||||
#if defined(PXR_STATIC)
|
||||
# define PLUG_API
|
||||
# define PLUG_API_TEMPLATE_CLASS(...)
|
||||
# define PLUG_API_TEMPLATE_STRUCT(...)
|
||||
# define PLUG_LOCAL
|
||||
#else
|
||||
# if defined(PLUG_EXPORTS)
|
||||
# define PLUG_API ARCH_EXPORT
|
||||
# define PLUG_API_TEMPLATE_CLASS(...) ARCH_EXPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define PLUG_API_TEMPLATE_STRUCT(...) ARCH_EXPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# else
|
||||
# define PLUG_API ARCH_IMPORT
|
||||
# define PLUG_API_TEMPLATE_CLASS(...) ARCH_IMPORT_TEMPLATE(class, __VA_ARGS__)
|
||||
# define PLUG_API_TEMPLATE_STRUCT(...) ARCH_IMPORT_TEMPLATE(struct, __VA_ARGS__)
|
||||
# endif
|
||||
# define PLUG_LOCAL ARCH_HIDDEN
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_INTERFACE_FACTORY_H
|
||||
#define PXR_BASE_PLUG_INTERFACE_FACTORY_H
|
||||
|
||||
/// \file plug/interfaceFactory.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/type.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
// For use by \c PLUG_REGISTER_INTERFACE_SINGLETON_TYPE.
|
||||
class Plug_InterfaceFactory {
|
||||
public:
|
||||
struct Base : public TfType::FactoryBase {
|
||||
public:
|
||||
virtual void* New() = 0;
|
||||
};
|
||||
|
||||
template <class Interface, class Implementation>
|
||||
struct SingletonFactory : public Base {
|
||||
public:
|
||||
virtual void* New()
|
||||
{
|
||||
static_assert(std::is_abstract<Interface>::value,
|
||||
"Interface type must be abstract.");
|
||||
static Implementation impl;
|
||||
return static_cast<Interface*>(&impl);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/// Defines the \c Interface \c TfType with a factory to return a
|
||||
/// \c Implementation singleton. This is suitable for use with
|
||||
/// \c PlugStaticInterface. \p Interface must be abstract and
|
||||
/// \p Implementation a concrete subclass of \p Interface. Note
|
||||
/// that this is a factory on \c Interface \b not \c Implementation.
|
||||
///
|
||||
/// The result of the factory is a singleton instance of \c Implementation
|
||||
/// and the client of TfType::GetFactory() must not destroy it.
|
||||
///
|
||||
/// Clients that want to create instances of types defined in a plugin
|
||||
/// but not added to the TfType system should create a singleton with
|
||||
/// factory methods to create those objects.
|
||||
#define PLUG_REGISTER_INTERFACE_SINGLETON_TYPE(Interface, Implementation) \
|
||||
TF_REGISTRY_FUNCTION(TfType) \
|
||||
{ \
|
||||
TfType::Define<Interface>() \
|
||||
.SetFactory<Plug_InterfaceFactory::SingletonFactory< \
|
||||
Interface, Implementation> >(); \
|
||||
}
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_PLUG_INTERFACE_FACTORY_H
|
||||
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_NOTICE_H
|
||||
#define PXR_BASE_PLUG_NOTICE_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/plug/api.h"
|
||||
#include "pxr/base/tf/declarePtrs.h"
|
||||
#include "pxr/base/tf/notice.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
TF_DECLARE_WEAK_PTRS(PlugPlugin);
|
||||
|
||||
/// \class PlugNotice
|
||||
/// Notifications sent by the Plug library.
|
||||
class PlugNotice
|
||||
{
|
||||
public:
|
||||
/// Base class for all Plug notices.
|
||||
class Base : public TfNotice
|
||||
{
|
||||
public:
|
||||
PLUG_API virtual ~Base();
|
||||
};
|
||||
|
||||
/// Notice sent after new plugins have been registered with the Plug
|
||||
/// registry.
|
||||
class DidRegisterPlugins : public Base
|
||||
{
|
||||
public:
|
||||
explicit DidRegisterPlugins(const PlugPluginPtrVector& newPlugins);
|
||||
PLUG_API virtual ~DidRegisterPlugins();
|
||||
|
||||
const PlugPluginPtrVector& GetNewPlugins() const
|
||||
{ return _plugins; }
|
||||
|
||||
private:
|
||||
PlugPluginPtrVector _plugins;
|
||||
};
|
||||
|
||||
private:
|
||||
PlugNotice();
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_PLUG_NOTICE_H
|
||||
@@ -0,0 +1,211 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_PLUGIN_H
|
||||
#define PXR_BASE_PLUG_PLUGIN_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/plug/api.h"
|
||||
|
||||
#include "pxr/base/js/types.h"
|
||||
#include "pxr/base/tf/declarePtrs.h"
|
||||
#include "pxr/base/tf/refPtr.h"
|
||||
#include "pxr/base/tf/weakPtr.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
TF_DECLARE_WEAK_AND_REF_PTRS(PlugPlugin);
|
||||
|
||||
class Plug_RegistrationMetadata;
|
||||
class TfType;
|
||||
|
||||
/// \class PlugPlugin
|
||||
///
|
||||
/// Defines an interface to registered plugins.
|
||||
///
|
||||
/// Plugins are registered using the interfaces in \c PlugRegistry.
|
||||
///
|
||||
/// For each registered plugin, there is an instance of
|
||||
/// \c PlugPlugin which can be used to load and unload
|
||||
/// the plugin and to retrieve information about the
|
||||
/// classes implemented by the plugin.
|
||||
///
|
||||
class PlugPlugin : public TfRefBase, public TfWeakBase {
|
||||
public:
|
||||
PLUG_API virtual ~PlugPlugin();
|
||||
|
||||
/// Loads the plugin.
|
||||
/// This is a noop if the plugin is already loaded.
|
||||
PLUG_API bool Load();
|
||||
|
||||
/// Returns \c true if the plugin is currently loaded. Resource
|
||||
/// plugins always report as loaded.
|
||||
PLUG_API bool IsLoaded() const;
|
||||
|
||||
#ifdef PXR_PYTHON_SUPPORT_ENABLED
|
||||
/// Returns \c true if the plugin is a python module.
|
||||
PLUG_API bool IsPythonModule() const;
|
||||
#endif // PXR_PYTHON_SUPPORT_ENABLED
|
||||
|
||||
/// Returns \c true if the plugin is resource-only.
|
||||
PLUG_API bool IsResource() const;
|
||||
|
||||
/// Returns the dictionary containing meta-data for the plugin.
|
||||
PLUG_API JsObject GetMetadata();
|
||||
|
||||
/// Returns the metadata sub-dictionary for a particular type.
|
||||
PLUG_API JsObject GetMetadataForType(const TfType &type);
|
||||
|
||||
/// Returns the dictionary containing the dependencies for the plugin.
|
||||
PLUG_API JsObject GetDependencies();
|
||||
|
||||
/// Returns true if \p type is declared by this plugin.
|
||||
/// If \p includeSubclasses is specified, also returns true if any
|
||||
/// subclasses of \p type have been declared.
|
||||
PLUG_API bool DeclaresType(const TfType& type, bool includeSubclasses = false) const;
|
||||
|
||||
/// Returns the plugin's name.
|
||||
std::string const &GetName() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
/// Returns the plugin's filesystem path.
|
||||
std::string const &GetPath() const {
|
||||
return _path;
|
||||
}
|
||||
|
||||
/// Returns the plugin's resources filesystem path.
|
||||
std::string const &GetResourcePath() const {
|
||||
return _resourcePath;
|
||||
}
|
||||
|
||||
/// Build a plugin resource path by returning a given absolute path or
|
||||
/// combining the plugin's resource path with a given relative path.
|
||||
PLUG_API std::string MakeResourcePath(const std::string& path) const;
|
||||
|
||||
/// Find a plugin resource by absolute or relative path optionally
|
||||
/// verifying that file exists. If verification fails an empty path
|
||||
/// is returned. Relative paths are relative to the plugin's resource
|
||||
/// path.
|
||||
PLUG_API std::string FindPluginResource(const std::string& path, bool verify = true) const;
|
||||
|
||||
private:
|
||||
enum _Type {
|
||||
LibraryType,
|
||||
#ifdef PXR_PYTHON_SUPPORT_ENABLED
|
||||
PythonType,
|
||||
#endif // PXR_PYTHON_SUPPORT_ENABLED
|
||||
ResourceType
|
||||
};
|
||||
|
||||
// Private ctor, plugins are constructed only by PlugRegistry.
|
||||
PLUG_LOCAL
|
||||
PlugPlugin(const std::string & path,
|
||||
const std::string & name,
|
||||
const std::string & resourcePath,
|
||||
const JsObject & plugInfo,
|
||||
_Type type);
|
||||
|
||||
PLUG_LOCAL
|
||||
static PlugPluginPtr _GetPluginForType(const TfType & type);
|
||||
|
||||
PLUG_LOCAL
|
||||
static void _RegisterAllPlugins();
|
||||
PLUG_LOCAL
|
||||
static PlugPluginPtr _GetPluginWithName(const std::string& name);
|
||||
PLUG_LOCAL
|
||||
static PlugPluginPtrVector _GetAllPlugins();
|
||||
|
||||
template <class PluginMap>
|
||||
PLUG_LOCAL
|
||||
static std::pair<PlugPluginPtr, bool>
|
||||
_NewPlugin(const Plug_RegistrationMetadata &metadata,
|
||||
_Type pluginType,
|
||||
const std::string& pluginCreationPath,
|
||||
PluginMap *allPluginsByNamePtr);
|
||||
|
||||
PLUG_LOCAL
|
||||
static std::pair<PlugPluginPtr, bool>
|
||||
_NewDynamicLibraryPlugin(const Plug_RegistrationMetadata& metadata);
|
||||
|
||||
#ifdef PXR_PYTHON_SUPPORT_ENABLED
|
||||
PLUG_LOCAL
|
||||
static std::pair<PlugPluginPtr, bool>
|
||||
_NewPythonModulePlugin(const Plug_RegistrationMetadata& metadata);
|
||||
#endif // PXR_PYTHON_SUPPORT_ENABLED
|
||||
|
||||
PLUG_LOCAL
|
||||
static std::pair<PlugPluginPtr, bool>
|
||||
_NewResourcePlugin(const Plug_RegistrationMetadata& metadata);
|
||||
|
||||
PLUG_LOCAL
|
||||
bool _Load();
|
||||
|
||||
PLUG_LOCAL
|
||||
void _DeclareAliases( TfType t, const JsObject & metadata );
|
||||
PLUG_LOCAL
|
||||
void _DeclareTypes();
|
||||
PLUG_LOCAL
|
||||
void _DeclareType(const std::string &name, const JsObject &dict);
|
||||
PLUG_LOCAL
|
||||
static void _DefineType( TfType t );
|
||||
|
||||
struct _SeenPlugins;
|
||||
PLUG_LOCAL
|
||||
bool _LoadWithDependents(_SeenPlugins * seenPlugins);
|
||||
|
||||
PLUG_LOCAL
|
||||
static void _UpdatePluginMaps( const TfType & baseType );
|
||||
|
||||
PLUG_LOCAL
|
||||
static constexpr char const *_GetPluginTypeDisplayName(_Type type);
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
std::string _path;
|
||||
std::string _resourcePath;
|
||||
JsObject _dict;
|
||||
void *_handle; // the handle returned by ArchLibraryOpen() is a void*
|
||||
std::atomic<bool> _isLoaded;
|
||||
_Type _type;
|
||||
|
||||
friend class PlugRegistry;
|
||||
};
|
||||
|
||||
/// Find a plugin's resource by absolute or relative path optionally
|
||||
/// verifying that file exists. If \c plugin is \c NULL or verification
|
||||
/// fails an empty path is returned. Relative paths are relative to the
|
||||
/// plugin's resource path.
|
||||
PLUG_API
|
||||
std::string
|
||||
PlugFindPluginResource(const PlugPluginPtr& plugin,
|
||||
const std::string& path, bool verify = true);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_PLUG_PLUGIN_H
|
||||
@@ -0,0 +1,488 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_REGISTRY_H
|
||||
#define PXR_BASE_PLUG_REGISTRY_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/plug/api.h"
|
||||
|
||||
#include "pxr/base/js/value.h"
|
||||
#include "pxr/base/tf/declarePtrs.h"
|
||||
#include "pxr/base/tf/hash.h"
|
||||
#include "pxr/base/tf/hashset.h"
|
||||
#include "pxr/base/tf/refPtr.h"
|
||||
#include "pxr/base/tf/singleton.h"
|
||||
#include "pxr/base/tf/type.h"
|
||||
#include "pxr/base/tf/weakBase.h"
|
||||
#include "pxr/base/tf/weakPtr.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
TF_DECLARE_WEAK_PTRS(PlugPlugin);
|
||||
class Plug_RegistrationMetadata;
|
||||
|
||||
/// \class PlugRegistry
|
||||
///
|
||||
/// Defines an interface for registering plugins.
|
||||
///
|
||||
/// PlugRegistry maintains a registry of plug-ins known to the system
|
||||
/// and provides an interface for base classes to load any plug-ins required
|
||||
/// to instantiate a subclass of a given type.
|
||||
///
|
||||
/// <h2>Defining a Base Class API</h2>
|
||||
///
|
||||
/// In order to use this facility you will generally provide a library
|
||||
/// which defines the API for a plug-in base class. This API
|
||||
/// will be sufficient for the application or framework to make use of
|
||||
/// custom subclasses that will be written by plug-in developers.
|
||||
///
|
||||
/// For example, if you have an image processing application, you might
|
||||
/// want to support plug-ins that implement image filters. You can define
|
||||
/// an abstract base class for image filters that declares the API your
|
||||
/// application will require image filters to implement; perhaps something
|
||||
/// simple like \ref plug_cppcode_PlugRegistry1 "C++ Code Example 1" (Doxygen only).
|
||||
///
|
||||
/// People writing custom filters would write a subclass of ImageFilter that
|
||||
/// overrides the two methods, implementing their own special filtering
|
||||
/// behavior.
|
||||
///
|
||||
/// \section plug_EnablingPlugins Enabling Plug-in Loading for the Base Class
|
||||
///
|
||||
/// In order for ImageFilter to be able to load plug-ins that implement
|
||||
/// these custom subclasses, it must be registered with the TfType system.
|
||||
///
|
||||
/// The ImageFilter base class, as was mentioned earlier, should be made
|
||||
/// available in a library that the application links with. This is done
|
||||
/// so that plug-ins that want to provide ImageFilters can also link with
|
||||
/// the library allowing them to subclass ImageFilter.
|
||||
///
|
||||
/// \section plug_RegisteringPlugins Registering Plug-ins
|
||||
///
|
||||
/// A plug-in developer can now write plug-ins with ImageFilter subclasses.
|
||||
/// Plug-ins can be implemented either as native dynamic libraries (either
|
||||
/// regular dynamic libraries or framework bundles) or as Python modules.
|
||||
///
|
||||
/// Plug-ins must be registered with the registry. All plugins are
|
||||
/// registered via RegisterPlugins(). Plug-in Python modules must be
|
||||
/// directly importable (in other words they must be able to be found in
|
||||
/// Python's module path.) Plugins are registered by providing a path or
|
||||
/// paths to JSON files that describe the location, structure and contents
|
||||
/// of the plugin. The standard name for these files in plugInfo.json.
|
||||
///
|
||||
/// Typically, the application that hosts plug-ins will locate and register
|
||||
/// plug-ins at startup.
|
||||
///
|
||||
/// The plug-in facility is lazy. It does not dynamically load code from
|
||||
/// plug-in bundles until that code is required.
|
||||
///
|
||||
/// \section plug_plugInfo plugInfo.json
|
||||
///
|
||||
/// A plugInfo.json file has the following structure:
|
||||
///
|
||||
/// \code
|
||||
/// {
|
||||
/// # Comments are allowed and indicated by a hash at the start of a
|
||||
/// # line or after spaces and tabs. They continue to the end of line.
|
||||
/// # Blank lines are okay, too.
|
||||
///
|
||||
/// # This is optional. It may contain any number of strings.
|
||||
/// # Paths may be absolute or relative.
|
||||
/// # Paths ending with slash have plugInfo.json appended automatically.
|
||||
/// # '*' may be used anywhere to match any character except slash.
|
||||
/// # '**' may be used anywhere to match any character including slash.
|
||||
/// "Includes": [
|
||||
/// "/absolute/path/to/plugInfo.json",
|
||||
/// "/absolute/path/to/custom.filename",
|
||||
/// "/absolute/path/to/directory/with/plugInfo/",
|
||||
/// "relative/path/to/plugInfo.json",
|
||||
/// "relative/path/to/directory/with/plugInfo/",
|
||||
/// "glob*/pa*th/*to*/*/plugInfo.json",
|
||||
/// "recursive/pa**th/**/"
|
||||
/// ],
|
||||
///
|
||||
/// # This is optional. It may contain any number of objects.
|
||||
/// "Plugins": [
|
||||
/// {
|
||||
/// # Type is required and may be "library", "python" or "resource".
|
||||
/// "Type": "library",
|
||||
///
|
||||
/// # Name is required. It should be the Python module name,
|
||||
/// # the shared library name, or a unique resource name.
|
||||
/// "Name": "myplugin",
|
||||
///
|
||||
/// # Root is optional. It defaults to ".".
|
||||
/// # This gives the path to the plugin as a whole if the plugin
|
||||
/// # has substructure. For Python it should be the directory
|
||||
/// # with the __init__.py file. The path is usually relative.
|
||||
/// "Root": ".",
|
||||
///
|
||||
/// # LibraryPath is required by Type "library" and unused
|
||||
/// # otherwise. It gives the path to the shared library
|
||||
/// # object, either absolute or relative to Root.
|
||||
/// "LibraryPath": "libmyplugin.so",
|
||||
///
|
||||
/// # ResourcePath is option. It defaults to ".".
|
||||
/// # This gives the path to the plugin's resources directory.
|
||||
/// # The path is either absolute or relative to Root.
|
||||
/// "ResourcePath": "resources",
|
||||
///
|
||||
/// # Info is required. It's described below.
|
||||
/// "Info": {
|
||||
/// # Plugin contents.
|
||||
/// }
|
||||
/// }
|
||||
/// ]
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// As a special case, if a plugInfo.json contains an object that doesn't
|
||||
/// have either the "Includes" or "Plugins" keys then it's as if the object
|
||||
/// was in a "Plugins" array.
|
||||
///
|
||||
/// \section plug_Advertising Advertising a Plug-in's Contents
|
||||
///
|
||||
/// Once the plug-ins are registered, the plug-in facility must also be
|
||||
/// able to tell what they contain. Specifically, it must be able to find
|
||||
/// out what subclasses of what plug-in base classes each plug-in contains.
|
||||
/// Plug-ins must advertise this information through their plugInfo.json file
|
||||
/// in the "Info" key. In the "Info" object there should be a key "Types"
|
||||
/// holding an object.
|
||||
///
|
||||
/// This "Types" object's keys are names of subclasses and its values are yet
|
||||
/// more objects (the subclass meta-data objects). The meta-data objects can
|
||||
/// contain arbitrary key-value pairs. The plug-in mechanism will look for a
|
||||
/// meta-data key called "displayName" whose value should be the display name
|
||||
/// of the subclass. The plug-in mechanism will look for a meta-data key
|
||||
/// called "bases" whose value should be an array of base class type names.
|
||||
///
|
||||
/// For example, a bundle that contains a subclass of ImageFilter might have
|
||||
/// a plugInfo.json that looks like the following example.
|
||||
///
|
||||
/// \code
|
||||
/// {
|
||||
/// "Types": {
|
||||
/// "MyCustomCoolFilter" : {
|
||||
/// "bases": ["ImageFilter"],
|
||||
/// "displayName": "Add Coolness to Image"
|
||||
/// # other arbitrary metadata for MyCustomCoolFilter here
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// What this says is that the plug-in contains a type called MyCustomCoolFilter
|
||||
/// which has a base class ImageFilter and that this subclass should be called
|
||||
/// "Add Coolness to Image" in user-visible contexts.
|
||||
///
|
||||
/// In addition to the "displayName" meta-data key which is actually
|
||||
/// known to the plug-in facility, you may put whatever other information
|
||||
/// you want into a class' meta-data dictionary. If your plug-in base class
|
||||
/// wants to define custom keys that it requires all subclasses to provide,
|
||||
/// you can do that. Or, if a plug-in writer wants to define their own keys
|
||||
/// that their code will look for at runtime, that is OK as well.
|
||||
///
|
||||
/// \section plug_subClasses Working with Subclasses of a Plug-in Base Class
|
||||
///
|
||||
/// Most code with uses types defined in plug-ins doesn't deal with
|
||||
/// the Plug API directly. Instead, the TfType interface is used
|
||||
/// to lookup types and to manufacture instances. The TfType interface
|
||||
/// will take care to load any required plugins.
|
||||
///
|
||||
/// To wrap up our example, the application that wants to actually use
|
||||
/// ImageFilter plug-ins would probably do a couple of things. First, it
|
||||
/// would get a list of available ImageFilters to present to the user.
|
||||
/// This could be accomplished as shown in
|
||||
/// \ref plug_cppcode_PlugRegistry2 "Python Code Example 2" (Doxygen only).
|
||||
///
|
||||
/// Then, when the user picks a filter from the list, it would manufacture
|
||||
/// and instance of the filter as shown in
|
||||
/// \ref plug_cppcode_PlugRegistry3 "Python Code Example 3" (Doxygen only).
|
||||
///
|
||||
/// As was mentioned earlier, this plug-in facility tries to be as lazy
|
||||
/// as possible about loading the code associated with plug-ins. To that end,
|
||||
/// loading of a plugin will be deferred until an instance of a type
|
||||
/// is manufactured which requires the plugin.
|
||||
///
|
||||
/// \section plug_MultipleSubclasses Multiple Subclasses of Multiple Plug-in Base Classes
|
||||
///
|
||||
/// It is possible for a bundle to implement multiple subclasses
|
||||
/// for a plug-in base class if desired. If you want to package half a dozen
|
||||
/// ImageFilter subclasses in one bundle, that will work fine. All must
|
||||
/// be declared in the plugInfo.json.
|
||||
///
|
||||
/// It is possible for there to be multiple classes in your
|
||||
/// application or framework that are plug-in base classes. Plug-ins that
|
||||
/// implement subclasses of any of these base classes can all coexist. And,
|
||||
/// it is possible to have subclasses of multiple plug-in base classes in the
|
||||
/// same bundle.
|
||||
///
|
||||
/// When putting multiple subclasses (of the same or different base classes)
|
||||
/// in a bundle, keep in mind that dynamic loading happens for the whole bundle
|
||||
/// the first time any subclass is needed, the whole bundle will be loaded.
|
||||
/// But this is generally not a big concern.
|
||||
///
|
||||
/// For example, say the example application also has a plug-in base class
|
||||
/// "ImageCodec" that allows people to add support for reading and writing
|
||||
/// other image formats. Imagine that you want to supply a plug-in that
|
||||
/// has two codecs and a filter all in a single plug-in. Your plugInfo.json
|
||||
/// "Info" object might look something like this example.
|
||||
///
|
||||
/// \code
|
||||
/// {
|
||||
/// "Types": {
|
||||
/// "MyTIFFCodec": {
|
||||
/// "bases": ["ImageCodec"],
|
||||
/// "displayName": "TIFF Image"
|
||||
/// },
|
||||
/// "MyJPEGCodec": {
|
||||
/// "bases": ["ImageCodec"],
|
||||
/// "displayName": "JPEG Image"
|
||||
/// },
|
||||
/// "MyCustomCoolFilter" : {
|
||||
/// "bases": ["ImageFilter"],
|
||||
/// "displayName": "Add Coolness to Image"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// \section plug_Dependencies Dependencies on Other Plug-ins
|
||||
///
|
||||
/// If you write a plug-in that has dependencies on another plug-in that you
|
||||
/// cannot (or do not want to) link against statically, you can declare
|
||||
/// the dependencies in your plug-in's plugInfo.json . A plug-in declares
|
||||
/// dependencies on other classes with a PluginDependencies key whose value
|
||||
/// is a dictionary. The keys of the dictionary are plug-in base class names
|
||||
/// and the values are arrays of subclass names.
|
||||
///
|
||||
/// The following example contains an example of a plug-in that depends on two
|
||||
/// classes from the plug-in in the previous example.
|
||||
///
|
||||
/// \code
|
||||
/// {
|
||||
/// "Types": {
|
||||
/// "UltraCoolFilter": {
|
||||
/// "bases": ["MyCustomCoolFilter"],
|
||||
/// "displayName": "Add Unbelievable Coolness to Image"
|
||||
/// # A subclass of MyCustomCoolFilter that also uses MyTIFFCodec
|
||||
/// }
|
||||
/// },
|
||||
/// "PluginDependencies": {
|
||||
/// "ImageFilter": ["MyCustomCoolFilter"],
|
||||
/// "ImageCodec": ["MyTIFFCodec"]
|
||||
/// }
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// The ImageFilter provided by the plug-in in this example depends on the
|
||||
/// other ImageFilter MyCoolImageFilter and the ImageCodec MyTIFFCodec.
|
||||
/// Before loading this plug-in, the plug-in facility will ensure that those
|
||||
/// two classes are present, loading the plug-in that contains them if needed.
|
||||
///
|
||||
/// \section plug_cppcode_PlugRegistry1 C++ Code Example 1
|
||||
/// \code
|
||||
/// // Declare a base class interface
|
||||
/// class ImageFilter {
|
||||
/// public:
|
||||
/// virtual bool CanFilterImage(const ImagePtr & inputImage) = 0;
|
||||
/// virtual ImagePtr FilterImage(const ImagePtr & inputImage) = 0;
|
||||
/// };
|
||||
/// \endcode
|
||||
///
|
||||
/// \section plug_cppcode_PlugRegistry2 Python Code Example 2
|
||||
/// \code
|
||||
/// # Get the names of derived types
|
||||
/// baseType = Tf.Type.Find(ImageFilter)
|
||||
/// if baseType:
|
||||
/// derivedTypes = baseType.GetAllDerived()
|
||||
/// derivedTypeNames = [ derived.typeName for derived in derivedTypes ]
|
||||
/// \endcode
|
||||
///
|
||||
/// \section plug_cppcode_PlugRegistry3 Python Code Example 3
|
||||
/// \code
|
||||
/// # Manufacture an instance of a derived type
|
||||
/// imageFilterType = Tf.Type.Find(ImageFilter)
|
||||
/// myFilterType = Tf.Type.FindByName('UltraCoolImageFilter')
|
||||
/// if myFilterType and myFilterType.IsA(imageFilterType):
|
||||
/// myFilter = myFilterType.Manufacture()
|
||||
/// \endcode
|
||||
///
|
||||
|
||||
class PlugRegistry : public TfWeakBase {
|
||||
PlugRegistry(PlugRegistry const &) = delete;
|
||||
PlugRegistry &operator=(PlugRegistry const &) = delete;
|
||||
public:
|
||||
typedef PlugRegistry This;
|
||||
typedef std::vector<TfType> TypeVector;
|
||||
|
||||
/// Returns the singleton \c PlugRegistry instance.
|
||||
PLUG_API
|
||||
static PlugRegistry & GetInstance();
|
||||
|
||||
/// Registers all plug-ins discovered at \a pathToPlugInfo. Sends
|
||||
/// PlugNotice::DidRegisterPlugins with any newly registered plugins.
|
||||
PLUG_API
|
||||
PlugPluginPtrVector RegisterPlugins(const std::string & pathToPlugInfo);
|
||||
|
||||
/// Registers all plug-ins discovered in any of \a pathsToPlugInfo. Sends
|
||||
/// PlugNotice::DidRegisterPlugins with any newly registered plugins.
|
||||
PLUG_API
|
||||
PlugPluginPtrVector
|
||||
RegisterPlugins(const std::vector<std::string> & pathsToPlugInfo);
|
||||
|
||||
/// Retrieve the \c TfType corresponding to the given \c name. See the
|
||||
/// documentation for \c TfType::FindByName for more information. Use this
|
||||
/// function if you expect that \c name may name a type provided by a
|
||||
/// plugin. Calling this function will incur plugin discovery (but not
|
||||
/// loading) if plugin discovery has not yet occurred.
|
||||
///
|
||||
/// Note that additional plugins may be registered during program runtime.
|
||||
/// \sa \ref Plug_Discovery
|
||||
PLUG_API
|
||||
static TfType FindTypeByName(std::string const &typeName);
|
||||
|
||||
/// Retrieve the \c TfType that derives from \c base and has the given alias
|
||||
/// or type name \c typeName. See the documentation for \c
|
||||
/// TfType::FindDerivedByName for more information. Use this function if
|
||||
/// you expect that the derived type may be provided by a plugin. Calling
|
||||
/// this function will incur plugin discovery (but not loading) if plugin
|
||||
/// discovery has not yet occurred.
|
||||
///
|
||||
/// Note that additional plugins may be registered during program runtime.
|
||||
/// \sa \ref Plug_Discovery
|
||||
PLUG_API
|
||||
static TfType
|
||||
FindDerivedTypeByName(TfType base, std::string const &typeName);
|
||||
|
||||
/// Retrieve the \c TfType that derives from \c Base and has the given alias
|
||||
/// or type name \c typeName. See the documentation for \c
|
||||
/// TfType::FindDerivedByName for more information. Use this function if
|
||||
/// you expect that the derived type may be provided by a plugin. Calling
|
||||
/// this function will incur plugin discovery (but not loading) if plugin
|
||||
/// discovery has not yet occurred.
|
||||
///
|
||||
/// Note that additional plugins may be registered during program runtime.
|
||||
/// \sa \ref Plug_Discovery
|
||||
template <class Base>
|
||||
static TfType
|
||||
FindDerivedTypeByName(std::string const &typeName) {
|
||||
return FindDerivedTypeByName(TfType::Find<Base>(), typeName);
|
||||
}
|
||||
|
||||
/// Return a vector of types derived directly from \a base. Use this
|
||||
/// function if you expect that plugins may provide types derived from \a
|
||||
/// base. Otherwise, use \a TfType::GetDirectlyDerivedTypes.
|
||||
///
|
||||
PLUG_API
|
||||
static std::vector<TfType>
|
||||
GetDirectlyDerivedTypes(TfType base);
|
||||
|
||||
/// Return the set of all types derived (directly or indirectly) from
|
||||
/// \a base. Use this function if you expect that plugins may provide types
|
||||
/// derived from \a base. Otherwise, use \a TfType::GetAllDerivedTypes.
|
||||
///
|
||||
/// Note that additional plugins may be registered during program runtime.
|
||||
/// \sa \ref Plug_Discovery
|
||||
PLUG_API
|
||||
static void
|
||||
GetAllDerivedTypes(TfType base, std::set<TfType> *result);
|
||||
|
||||
/// Return the set of all types derived (directly or indirectly) from
|
||||
/// \a Base. Use this function if you expect that plugins may provide types
|
||||
/// derived from \a base. Otherwise, use \a TfType::GetAllDerivedTypes.
|
||||
///
|
||||
/// Note that additional plugins may be registered during program runtime.
|
||||
/// \sa \ref Plug_Discovery
|
||||
template <class Base>
|
||||
static void
|
||||
GetAllDerivedTypes(std::set<TfType> *result) {
|
||||
return GetAllDerivedTypes(TfType::Find<Base>(), result);
|
||||
}
|
||||
|
||||
/// Returns the plug-in for the given type, or a null pointer if there is no
|
||||
/// registered plug-in.
|
||||
PLUG_API
|
||||
PlugPluginPtr GetPluginForType(TfType t) const;
|
||||
|
||||
/// Returns all registered plug-ins. Note that additional plugins may be
|
||||
/// registered during program runtime. \sa \ref Plug_Discovery
|
||||
PLUG_API
|
||||
PlugPluginPtrVector GetAllPlugins() const;
|
||||
|
||||
/// Returns a plugin with the specified library name. Note that additional
|
||||
/// plugins may be registered during program runtime.
|
||||
/// \sa \ref Plug_Discovery
|
||||
PLUG_API
|
||||
PlugPluginPtr GetPluginWithName(const std::string& name) const;
|
||||
|
||||
/// Looks for a string associated with \a type and \a key and returns it, or
|
||||
/// an empty string if \a type or \a key are not found.
|
||||
PLUG_API
|
||||
std::string GetStringFromPluginMetaData(TfType type,
|
||||
const std::string &key) const;
|
||||
|
||||
/// Looks for a JsValue associated with \a type and \a key and returns it,
|
||||
/// or a null JsValue if \a type or \a key are not found.
|
||||
PLUG_API
|
||||
JsValue GetDataFromPluginMetaData(TfType type,
|
||||
const std::string &key) const;
|
||||
|
||||
private:
|
||||
// Private ctor and dtor since this is a constructed as a singleton.
|
||||
PLUG_LOCAL
|
||||
PlugRegistry();
|
||||
|
||||
// Registers all plug-ins discovered in any of \a pathsToPlugInfo
|
||||
// but does not send a notice. The priority order of paths is honored if
|
||||
// pathsAreOrdered.
|
||||
PLUG_LOCAL
|
||||
PlugPluginPtrVector
|
||||
_RegisterPlugins(const std::vector<std::string>& pathsToPlugInfo,
|
||||
bool pathsAreOrdered);
|
||||
|
||||
template <class ConcurrentVector>
|
||||
PLUG_LOCAL
|
||||
void _RegisterPlugin(const Plug_RegistrationMetadata&,
|
||||
ConcurrentVector *newPlugins);
|
||||
PLUG_LOCAL
|
||||
bool _InsertRegisteredPluginPath(const std::string &path);
|
||||
|
||||
friend class TfSingleton< PlugRegistry >;
|
||||
friend class PlugPlugin; // For _RegisterPlugins().
|
||||
|
||||
private:
|
||||
TfHashSet<std::string, TfHash> _registeredPluginPaths;
|
||||
|
||||
std::mutex _mutex;
|
||||
};
|
||||
|
||||
PLUG_API_TEMPLATE_CLASS(TfSingleton<PlugRegistry>);
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_PLUG_REGISTRY_H
|
||||
@@ -0,0 +1,220 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_STATIC_INTERFACE_H
|
||||
#define PXR_BASE_PLUG_STATIC_INTERFACE_H
|
||||
|
||||
/// \file plug/staticInterface.h
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/plug/api.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <type_traits>
|
||||
#include <typeinfo>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
// Base class for common stuff.
|
||||
class Plug_StaticInterfaceBase {
|
||||
public:
|
||||
/// Returns \c true if we've tried to initialize the interface pointer,
|
||||
/// even if we failed. This will not attempt to load the plugin or
|
||||
/// initialize the interface pointer.
|
||||
bool IsInitialized() const
|
||||
{
|
||||
return _initialized;
|
||||
}
|
||||
|
||||
#if !defined(doxygen)
|
||||
typedef void* Plug_StaticInterfaceBase::*UnspecifiedBoolType;
|
||||
#endif
|
||||
|
||||
protected:
|
||||
PLUG_API
|
||||
void _LoadAndInstantiate(const std::type_info& type) const;
|
||||
|
||||
protected:
|
||||
// POD types only!
|
||||
mutable std::atomic<bool> _initialized;
|
||||
mutable void* _ptr;
|
||||
};
|
||||
|
||||
/// \class PlugStaticInterface
|
||||
///
|
||||
/// Provides access to an interface into a plugin.
|
||||
///
|
||||
/// A plugin can provide one or more interface types through which clients
|
||||
/// can access the plugin's full functionality without needing to link
|
||||
/// against the plugin (if you had to link against it, it wouldn't be a
|
||||
/// plugin). This is a convenience; you can achieve the same effect
|
||||
/// with TfType::GetFactory().
|
||||
///
|
||||
/// Typical usage is:
|
||||
/// \code
|
||||
/// #include "path/to/SomePlugin.h"
|
||||
/// #include "pxr/base/plug/staticInterface.h"
|
||||
///
|
||||
/// static PlugStaticInterface<SomePluginInterface> ptr;
|
||||
///
|
||||
/// void MyFunction() {
|
||||
/// if (ptr) {
|
||||
/// // Plugin is available.
|
||||
/// ptr->MakePluginDoSomething();
|
||||
/// }
|
||||
/// else {
|
||||
/// // Plugin is not available. (An error will have been reported
|
||||
/// // the first time through.)
|
||||
/// }
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
/// The interface must be defined correctly. In particular, it must have
|
||||
///
|
||||
/// \li no data members (static or otherwise),
|
||||
/// \li no static member functions,
|
||||
/// \li no non-virtual member functions,
|
||||
/// \li only pure virtual member functions,
|
||||
/// \li no constructors except an inline protected default that does nothing
|
||||
/// (or no constructors at all,)
|
||||
/// \li a virtual destructor as the first member that \e must be defined
|
||||
/// \e inline with an empty body even though inline virtual destructors
|
||||
/// are contrary to conventional practice.
|
||||
///
|
||||
/// The last requirement causes the compiler to emit a copy of the interface
|
||||
/// typeinfo into clients of the interface. This typeinfo is required by
|
||||
/// PlugStaticInterface internals to perform the appropriate plugin metadata
|
||||
/// search for the interface type. Note that due to limitations in the GCC
|
||||
/// C++ ABI an inline virtual destructor may prevent dynamic_cast<> and typeid()
|
||||
/// from working correctly; do not use those on the interface type.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// \code
|
||||
/// class SomePluginInterface {
|
||||
/// public:
|
||||
/// virtual ~SomePluginInterface() { }
|
||||
///
|
||||
/// virtual bool MakePluginDoSomething() const = 0;
|
||||
///
|
||||
/// protected:
|
||||
/// SomePluginInterface() { }
|
||||
/// };
|
||||
/// \endcode
|
||||
/// Note that interface types do not share a common base class.
|
||||
///
|
||||
/// For the plugin to work, there must be a concrete implementation of the
|
||||
/// interface type, the interface type must be in plugInfo file, and the
|
||||
/// interface type must be registered with TfType using
|
||||
/// \c PLUG_REGISTER_INTERFACE_SINGLETON_TYPE:
|
||||
/// \code
|
||||
/// #include "path/to/SomePlugin.h"
|
||||
/// #include "pxr/base/plug/interfaceFactory.h"
|
||||
/// #include <iostream>
|
||||
/// class SomePluginImplementation : public SomePluginInterface {
|
||||
/// public:
|
||||
/// virtual bool MakePluginDoSomething() const {
|
||||
/// return std::cout << "Plugin did something" << std::endl;
|
||||
/// }
|
||||
/// };
|
||||
/// PLUG_REGISTER_INTERFACE_SINGLETON_TYPE(SomePluginInterface,
|
||||
/// SomePluginImplementation)
|
||||
/// \endcode
|
||||
/// This causes TfType::Find<SomePluginInterface>::Manufacture() to return
|
||||
/// a pointer to a singleton instance of SomePluginImplementation.
|
||||
///
|
||||
/// Note that only SomePluginInterface needs to be registered in the plugInfo
|
||||
/// file and with TfType; other types provided by the plugin need only be
|
||||
/// defined in SomePlugin.h. In addition, SomePluginInterface can provide
|
||||
/// access to free functions in SomePlugin; clients would otherwise have to use
|
||||
/// \c dlsym() to access free functions in the plugin.
|
||||
///
|
||||
/// Warning: the \c PlugStaticInterface construct relies upon zero-initialization
|
||||
/// of global data: therefore, you can only use this structure for static data
|
||||
/// member of classes, variables declared at file-scope, or variables declared
|
||||
/// static within a function. Do \e not declare a \c PlugStaticInterface object
|
||||
/// as a local variable, as a member of a class or structure, or as a function
|
||||
/// parameter.
|
||||
///
|
||||
template <class Interface>
|
||||
class PlugStaticInterface : private Plug_StaticInterfaceBase {
|
||||
public:
|
||||
static_assert(std::is_abstract<Interface>::value,
|
||||
"Interface type must be abstract.");
|
||||
|
||||
typedef PlugStaticInterface<Interface> This;
|
||||
|
||||
using Plug_StaticInterfaceBase::IsInitialized;
|
||||
|
||||
/// Load and instantiate then return \c true if the interface is valid,
|
||||
/// \c false otherwise.
|
||||
operator UnspecifiedBoolType() const
|
||||
{
|
||||
return _GetPtr() ? &This::_ptr : nullptr;
|
||||
}
|
||||
|
||||
/// Load and instantiate then return \c false if the interface is valid,
|
||||
/// \c true otherwise.
|
||||
bool operator!() const
|
||||
{
|
||||
return !*this;
|
||||
}
|
||||
|
||||
/// Returns the interface pointer, loading the plugin if necessary.
|
||||
/// Returns \c nullptr if the interface could not be initialized.
|
||||
Interface* Get() const
|
||||
{
|
||||
return _GetPtr();
|
||||
}
|
||||
|
||||
/// Returns the interface pointer, loading the plugin if necessary.
|
||||
/// Returns \c nullptr if the interface could not be initialized.
|
||||
Interface* operator->() const
|
||||
{
|
||||
return _GetPtr();
|
||||
}
|
||||
|
||||
/// Returns the interface pointer as a reference, loading the plugin
|
||||
/// if necessary. Returns \c nullptr if the interface could not be
|
||||
/// initialized.
|
||||
Interface& operator*() const
|
||||
{
|
||||
return *_GetPtr();
|
||||
}
|
||||
|
||||
private:
|
||||
Interface* _GetPtr() const
|
||||
{
|
||||
if (!_initialized) {
|
||||
_LoadAndInstantiate(typeid(Interface));
|
||||
}
|
||||
|
||||
// XXX: We must assume _ptr has the right type since we have
|
||||
// no common base class to dynamic_cast<> from.
|
||||
return static_cast<Interface*>(_ptr);
|
||||
}
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_PLUG_STATIC_INTERFACE_H
|
||||
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// Copyright 2016 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_PLUG_THIS_PLUGIN_H
|
||||
#define PXR_BASE_PLUG_THIS_PLUGIN_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/plug/registry.h"
|
||||
#include "pxr/base/tf/preprocessorUtilsLite.h"
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// Returns a plugin registered with the name of the current library (uses the
|
||||
/// define for MFB_PACKAGE_NAME). Note that plugin registration occurs as a
|
||||
/// side effect of using this macro, at the point in time the code at the
|
||||
/// macro site is invoked.
|
||||
#define PLUG_THIS_PLUGIN \
|
||||
PlugRegistry::GetInstance().GetPluginWithName(\
|
||||
TF_PP_STRINGIZE(MFB_PACKAGE_NAME))
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif // PXR_BASE_PLUG_THIS_PLUGIN_H
|
||||
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// Copyright 2019 Pixar
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
||||
// with the following modification; you may not use this file except in
|
||||
// compliance with the Apache License and the following modification to it:
|
||||
// Section 6. Trademarks. is deleted and replaced with:
|
||||
//
|
||||
// 6. Trademarks. This License does not grant permission to use the trade
|
||||
// names, trademarks, service marks, or product names of the Licensor
|
||||
// and its affiliates, except as required to comply with Section 4(c) of
|
||||
// the License and to reproduce the content of the NOTICE file.
|
||||
//
|
||||
// You may obtain a copy of the Apache License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the Apache License with the above modification is
|
||||
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the Apache License for the specific
|
||||
// language governing permissions and limitations under the Apache License.
|
||||
//
|
||||
#ifndef PXR_BASE_TF_ANY_UNIQUE_PTR_H
|
||||
#define PXR_BASE_TF_ANY_UNIQUE_PTR_H
|
||||
|
||||
#include "pxr/pxr.h"
|
||||
#include "pxr/base/tf/api.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
PXR_NAMESPACE_OPEN_SCOPE
|
||||
|
||||
/// A simple type-erased container that provides only destruction, moves and
|
||||
/// immutable, untyped access to the held value.
|
||||
///
|
||||
/// There are a couple of facilities that provide fallback or default values
|
||||
/// in error cases. TfAnyUniquePtr exists to hold these oft-instantiated but
|
||||
/// rarely accessed values. As such, its design prioritizes compile-time
|
||||
/// overhead over runtime performance and avoids clever metaprogramming.
|
||||
/// Please resist the urge to add functionality to this class (e.g. small
|
||||
/// object optimization, boost::python interoperability.)
|
||||
class TfAnyUniquePtr
|
||||
{
|
||||
public:
|
||||
template <typename T>
|
||||
static TfAnyUniquePtr New() {
|
||||
static_assert(!std::is_array<T>::value, "Array types not supported");
|
||||
return TfAnyUniquePtr(new T());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static TfAnyUniquePtr New(T const &v) {
|
||||
static_assert(!std::is_array<T>::value, "Array types not supported");
|
||||
return TfAnyUniquePtr(new T(v));
|
||||
}
|
||||
|
||||
TfAnyUniquePtr(TfAnyUniquePtr &&other)
|
||||
: _ptr(other._ptr)
|
||||
, _delete(other._delete)
|
||||
{
|
||||
other._ptr = nullptr;
|
||||
// We don't set other._delete to nullptr here on purpose. Invoking
|
||||
// delete on a null pointer is not an error so if we can ensure that
|
||||
// _delete is never null we can call it unconditionally.
|
||||
}
|
||||
|
||||
TfAnyUniquePtr& operator=(TfAnyUniquePtr &&other) {
|
||||
if (this != &other) {
|
||||
_delete(_ptr);
|
||||
_ptr = other._ptr;
|
||||
_delete = other._delete;
|
||||
other._ptr = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
TfAnyUniquePtr(TfAnyUniquePtr const&) = delete;
|
||||
TfAnyUniquePtr& operator=(TfAnyUniquePtr const&) = delete;
|
||||
|
||||
~TfAnyUniquePtr() {
|
||||
_delete(_ptr);
|
||||
}
|
||||
|
||||
/// Return a pointer to the owned object.
|
||||
void const *Get() const {
|
||||
return _ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
explicit TfAnyUniquePtr(T const *ptr)
|
||||
: _ptr(ptr)
|
||||
, _delete(&_Delete<T>)
|
||||
{}
|
||||
|
||||
template <typename T>
|
||||
static void _Delete(void const *ptr) {
|
||||
delete static_cast<T const *>(ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
void const *_ptr;
|
||||
void (*_delete)(void const *);
|
||||
};
|
||||
|
||||
PXR_NAMESPACE_CLOSE_SCOPE
|
||||
|
||||
#endif
|
||||
Loaded 100 of 15675 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user