diff --git a/library/phx/resources/types/lookup_table.cpp b/library/phx/resources/types/lookup_table.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d27c03f27ee1b529b7296d9db3be258b4a0c445 --- /dev/null +++ b/library/phx/resources/types/lookup_table.cpp @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// Project Phoenix +// +// Copyright (c) 2017-2018 RWTH Aachen University, Germany, +// Virtual Reality & Immersive Visualization Group. +//------------------------------------------------------------------------------ +// License +// +// Licensed under the 3-Clause BSD License (the "License"); +// you may not use this file except in compliance with the License. +// See the file LICENSE for the full text. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//------------------------------------------------------------------------------ + +#include "phx/resources/types/lookup_table.hpp" + +#include <memory> + +namespace phx { +LookupTable::LookupTable(const TransferFunction& transfer_function, + int num_colors) + : min_key_(transfer_function.GetEntries().front().scalar), + max_key_(transfer_function.GetEntries().back().scalar), + transfer_function_(transfer_function) { + Generate(num_colors); +} + +void LookupTable::Generate(int num_colors) { + float step = (max_key_ - min_key_) / (num_colors - 1); + for (auto i = 0; i < num_colors; ++i) { + lookup_table_.push_back( + transfer_function_.Interpolate(min_key_ + i * step)); + } +} +glm::vec4 LookupTable::GetColor(int index) const { + return lookup_table_.at(index); +} + +std::size_t LookupTable::GetIndex(float key) const { + return static_cast<std::size_t>( + std::round((GetSize() - 1) * ((key - min_key_) / (max_key_ - min_key_)))); +} + +std::size_t LookupTable::GetSize() const { return lookup_table_.size(); } + +std::unique_ptr<gl::texture_1d> LookupTable::GetTexture1D() const { + auto lookup_texture = std::make_unique<gl::texture_1d>(); + lookup_texture->set_storage(0, GL_RGBA, + static_cast<GLsizei>(lookup_table_.size())); + lookup_texture->set_sub_image(0, 0, + static_cast<GLsizei>(lookup_table_.size()), + GL_RGBA8, GL_FLOAT, lookup_table_.data()); + return lookup_texture; +} + +} // namespace phx diff --git a/library/phx/resources/types/lookup_table.hpp b/library/phx/resources/types/lookup_table.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ac02752cca598fc5fac4d9839d04cffdfb804018 --- /dev/null +++ b/library/phx/resources/types/lookup_table.hpp @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// Project Phoenix +// +// Copyright (c) 2017-2018 RWTH Aachen University, Germany, +// Virtual Reality & Immersive Visualization Group. +//------------------------------------------------------------------------------ +// License +// +// Licensed under the 3-Clause BSD License (the "License"); +// you may not use this file except in compliance with the License. +// See the file LICENSE for the full text. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//------------------------------------------------------------------------------ + +#ifndef LIBRARY_PHX_RESOURCES_TYPES_LOOKUP_TABLE_HPP_ +#define LIBRARY_PHX_RESOURCES_TYPES_LOOKUP_TABLE_HPP_ + +#include <memory> +#include <vector> + +#include "phx/resources/types/shader_source.hpp" +#include "phx/resources/types/transfer_function.hpp" +#include "phx/suppress_warnings.hpp" + +SUPPRESS_WARNINGS_BEGIN +#include "gl/texture.hpp" +#include "glm/detail/type_vec4.hpp" +SUPPRESS_WARNINGS_END + +namespace phx { + +class PHOENIX_EXPORT LookupTable { + public: + LookupTable(const TransferFunction& transfer_function, int num_colors); + LookupTable(const LookupTable&) = default; + LookupTable(LookupTable&&) = default; + ~LookupTable() = default; + + std::size_t GetIndex(float key) const; + glm::vec4 GetColor(int index) const; + std::size_t GetSize() const; + + std::unique_ptr<gl::texture_1d> GetTexture1D() const; + + private: + void Generate(int num_colors); + + float min_key_; + float max_key_; + std::vector<glm::vec4> lookup_table_; + const TransferFunction transfer_function_; +}; +} // namespace phx + +#endif // LIBRARY_PHX_RESOURCES_TYPES_LOOKUP_TABLE_HPP_ diff --git a/tests/src/test_lookup_table.cpp b/tests/src/test_lookup_table.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3def80b05714dfdb8d7906a965ce35b19aa56ce --- /dev/null +++ b/tests/src/test_lookup_table.cpp @@ -0,0 +1,66 @@ +//------------------------------------------------------------------------------ +// Project Phoenix +// +// Copyright (c) 2017-2018 RWTH Aachen University, Germany, +// Virtual Reality & Immersive Visualization Group. +//------------------------------------------------------------------------------ +// License +// +// Licensed under the 3-Clause BSD License (the "License"); +// you may not use this file except in compliance with the License. +// See the file LICENSE for the full text. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//------------------------------------------------------------------------------ + +#include "phx/resources/types/lookup_table.hpp" +#include "phx/resources/types/transfer_function.hpp" + +#include "catch/catch.hpp" + +SCENARIO("Creating and accesing a Lookup Table", "[phx][phx::LookupTable]") { + GIVEN("A test transfer function") { + phx::TransferFunction transfer_function; + glm::vec4 red_color{1.0f, 0.0f, 0.0f, 0.0f}; + glm::vec4 black_color{0.0f, 0.0f, 0.0f, 0.0f}; + transfer_function.SetEntries({{0.0f, red_color}, {1.0f, black_color}}); + + WHEN("We create Lookup Table from the given Transfer function") { + phx::LookupTable lut(transfer_function, 11); + + THEN("The Lookup Table is generated correctly") { + REQUIRE(lut.GetColor(0) == red_color); + REQUIRE(lut.GetColor(2) == glm::vec4(0.8f, 0.0f, 0.0f, 0.0f)); + REQUIRE(lut.GetColor(5) == glm::vec4(0.5f, 0.0f, 0.0f, 0.0f)); + REQUIRE(lut.GetColor(7) == glm::vec4(0.3f, 0.0f, 0.0f, 0.0f)); + REQUIRE(lut.GetColor(10) == black_color); + } + + THEN("Keys can be converted indices") { + REQUIRE(lut.GetIndex(0.0f) == 0); + REQUIRE(lut.GetIndex(1.0f) == 10); + REQUIRE(lut.GetIndex(0.56f) == 6); + } + } + + WHEN("We ask for a nearest neighbor interpolated Lookup Table") { + transfer_function.SetMode( + phx::TransferFunction::InterpolationMode::NEAREST_NEIGHBOR); + phx::LookupTable lut(transfer_function, 11); + THEN("The Lookuptable is generated correctly") { + REQUIRE(lut.GetColor(0) == red_color); + REQUIRE(lut.GetColor(2) == red_color); + REQUIRE(lut.GetColor(5) == black_color); + REQUIRE(lut.GetColor(7) == black_color); + REQUIRE(lut.GetColor(10) == black_color); + } + } + } +}