/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 ** 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 VEC3_H #define VEC3_H #include #include // Implementes a class to represent the pixel value in a 3-dimensional color // space. The colors are all represented by int as it is mostly used as RGB. template class Vec3{ public: Vec3(T inputRed, T inputGreen, T inputBlue) { mRed = inputRed; mGreen = inputGreen; mBlue = inputBlue; } Vec3() {} template inline Vec3 operator+ (const Vec3 ¶m) const { Vec3 temp(mRed + param.r(), mGreen + param.g(), mBlue + param.b()); return temp; } inline Vec3 operator- (const Vec3 ¶m) const { Vec3 temp(mRed - param.r(), mGreen - param.g(), mBlue - param.b()); return temp; } inline Vec3 operator* (const int param) const { Vec3 temp(mRed * param, mGreen * param, mBlue * param); return temp; } template inline Vec3 operator* (const Vec3 ¶m) const { Vec3 temp(mRed * static_cast(param.r()), mGreen * static_cast(param.g()), mBlue * static_cast(param.b())); return temp; } inline Vec3 operator/ (const int param) const { Vec3 temp; assert(param != 0); temp.set(static_cast(mRed) / static_cast(param), static_cast(mGreen) / static_cast(param), static_cast(mBlue) / static_cast(param)); return temp; } template inline Vec3 operator/ (const Vec3 ¶m) const { Vec3 temp; assert((param.r() != 0.f) && (param.g() != 0.f) && (param.b() != 0.f)); temp.set(static_cast(mRed) / static_cast(param.r()), static_cast(mGreen) / static_cast(param.g()), static_cast(mBlue) / static_cast(param.b())); return temp; } template float squareDistance(const Vec3 ¶m) const { float difference = 0.f; difference = static_cast(mRed - param.r()) * static_cast(mRed - param.r()) + static_cast(mGreen - param.g()) * static_cast(mGreen - param.g()) + static_cast(mBlue - param.b()) * static_cast(mBlue - param.b()); return difference; } // Assumes conversion from sRGB to luminance. float convertToLuminance() const { return (0.299 * static_cast(mRed) + 0.587 * static_cast(mGreen) + 0.114 * static_cast(mBlue)); } inline T r() const { return mRed; } inline T g() const { return mGreen; } inline T b() const { return mBlue; } inline void set(const T inputRed, const T inputGreen, const T inputBlue){ mRed = inputRed; mGreen = inputGreen; mBlue = inputBlue; } private: T mRed; T mGreen; T mBlue; }; typedef Vec3 Vec3i; typedef Vec3 Vec3f; #endif