color.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "cartographer/io/color.h"
00018 
00019 #include <cmath>
00020 
00021 #include "cartographer/common/port.h"
00022 #include "glog/logging.h"
00023 
00024 namespace cartographer {
00025 namespace io {
00026 
00027 namespace {
00028 
00029 constexpr float kInitialHue = 0.69f;
00030 constexpr float kSaturation = 0.85f;
00031 constexpr float kValue = 0.77f;
00032 
00033 FloatColor HsvToRgb(const float h, const float s, const float v) {
00034   const float h_6 = (h == 1.f) ? 0.f : 6 * h;
00035   const int h_i = std::floor(h_6);
00036   const float f = h_6 - h_i;
00037 
00038   const float p = v * (1.f - s);
00039   const float q = v * (1.f - f * s);
00040   const float t = v * (1.f - (1.f - f) * s);
00041 
00042   if (h_i == 0) {
00043     return {{v, t, p}};
00044   } else if (h_i == 1) {
00045     return {{q, v, p}};
00046   } else if (h_i == 2) {
00047     return {{p, v, t}};
00048   } else if (h_i == 3) {
00049     return {{p, q, v}};
00050   } else if (h_i == 4) {
00051     return {{t, p, v}};
00052   } else if (h_i == 5) {
00053     return {{v, p, q}};
00054   } else {
00055     return {{0.f, 0.f, 0.f}};
00056   }
00057 }
00058 
00059 }  // namespace
00060 
00061 FloatColor GetColor(int id) {
00062   CHECK_GE(id, 0);
00063   // Uniform color sampling using the golden ratio from
00064   // http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
00065   constexpr float kGoldenRatioConjugate = 0.6180339887498949f;
00066   const float hue = std::fmod(kInitialHue + kGoldenRatioConjugate * id, 1.f);
00067   return HsvToRgb(hue, kSaturation, kValue);
00068 }
00069 
00070 }  // namespace io
00071 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35