1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "ConfigManager.h"
17 
18 #include "json/json.h"
19 
20 #include <fstream>
21 #include <math.h>
22 #include <assert.h>
23 
24 
25 static const float kDegreesToRadians = M_PI / 180.0f;
26 
27 
normalizeToPlusMinus180degrees(float theta)28 static float normalizeToPlusMinus180degrees(float theta) {
29     const float wraps = floor((theta+180.0f) / 360.0f);
30     return theta - wraps*360.0f;
31 }
32 
33 
readChildNodeAsFloat(const char * groupName,const Json::Value & parentNode,const char * childName,float * value)34 static bool readChildNodeAsFloat(const char* groupName,
35                                  const Json::Value& parentNode,
36                                  const char* childName,
37                                  float* value) {
38     // Must have a place to put the value!
39     assert(value);
40 
41     Json::Value childNode = parentNode[childName];
42     if (!childNode.isNumeric()) {
43         printf("Missing or invalid field %s in record %s", childName, groupName);
44         return false;
45     }
46 
47     *value = childNode.asFloat();
48     return true;
49 }
50 
51 
initialize(const char * configFileName)52 bool ConfigManager::initialize(const char* configFileName)
53 {
54     bool complete = true;
55 
56     // Set up a stream to read in the input file
57     std::ifstream configStream(configFileName);
58 
59     // Parse the stream into JSON objects
60     Json::CharReaderBuilder builder;
61     builder["collectComments"] = false;
62     std::string errorMessage;
63     Json::Value rootNode;
64     bool parseOk = Json::parseFromStream(builder, configStream, &rootNode, &errorMessage);
65     if (!parseOk) {
66         printf("Failed to read configuration file %s\n", configFileName);
67         printf("%s\n", errorMessage.c_str());
68         return false;
69     }
70 
71 
72     //
73     // Read car information
74     //
75     {
76         Json::Value car = rootNode["car"];
77         if (!car.isObject()) {
78             printf("Invalid configuration format -- we expect a car description\n");
79             return false;
80         }
81         complete &= readChildNodeAsFloat("car", car, "width",       &mCarWidth);
82         complete &= readChildNodeAsFloat("car", car, "wheelBase",   &mWheelBase);
83         complete &= readChildNodeAsFloat("car", car, "frontExtent", &mFrontExtent);
84         complete &= readChildNodeAsFloat("car", car, "rearExtent",  &mRearExtent);
85     }
86 
87 
88     //
89     // Read display layout information
90     //
91     {
92         Json::Value displayArray = rootNode["displays"];
93         if (!displayArray.isArray()) {
94             printf("Invalid configuration format -- we expect an array of displays\n");
95             return false;
96         }
97 
98         mDisplays.reserve(displayArray.size());
99         for (auto&& node : displayArray) {
100             DisplayInfo info;
101             info.port = node.get("displayPort", 0).asUInt();
102             info.function = node.get("function", "").asCString();
103             info.frontRangeInCarSpace = node.get("frontRange", -1).asFloat();
104             info.rearRangeInCarSpace = node.get("rearRange", -1).asFloat();
105 
106             mDisplays.emplace_back(info);
107         }
108     }
109 
110 
111     //
112     // Car top view texture properties for top down view
113     //
114     {
115         Json::Value graphicNode = rootNode["graphic"];
116         if (!graphicNode.isObject()) {
117             printf("Invalid configuration format -- we expect a graphic description\n");
118             return false;
119         }
120         complete &= readChildNodeAsFloat("graphic", graphicNode, "frontPixel", &mCarGraphicFrontPixel);
121         complete &= readChildNodeAsFloat("display", graphicNode, "rearPixel",  &mCarGraphicRearPixel);
122     }
123 
124 
125     //
126     // Read camera information
127     // NOTE:  Missing positions and angles are not reported, but instead default to zero
128     //
129     {
130         Json::Value cameraArray = rootNode["cameras"];
131         if (!cameraArray.isArray()) {
132             printf("Invalid configuration format -- we expect an array of cameras\n");
133             return false;
134         }
135 
136         mCameras.reserve(cameraArray.size());
137         for (auto&& node: cameraArray) {
138             // Get data from the configuration file
139             Json::Value nameNode = node.get("cameraId", "MISSING");
140             const char *cameraId = nameNode.asCString();
141 
142             Json::Value usageNode = node.get("function", "");
143             const char *function = usageNode.asCString();
144 
145             float yaw   = node.get("yaw", 0).asFloat();
146             float pitch = node.get("pitch", 0).asFloat();
147             float roll  = node.get("roll", 0).asFloat();
148             float hfov  = node.get("hfov", 0).asFloat();
149             float vfov  = node.get("vfov", 0).asFloat();
150             bool  hflip = node.get("hflip", false).asBool();
151             bool  vflip = node.get("vflip", false).asBool();
152 
153             // Wrap the direction angles to be in the 180deg to -180deg range
154             // Rotate 180 in yaw if necessary to flip the pitch into the +/-90degree range
155             pitch = normalizeToPlusMinus180degrees(pitch);
156             if (pitch > 90.0f) {
157                 yaw += 180.0f;
158                 pitch = 180.0f - pitch;
159             }
160             if (pitch < -90.0f) {
161                 yaw += 180.0f;
162                 pitch = -180.0f + pitch;
163             }
164             yaw = normalizeToPlusMinus180degrees(yaw);
165             roll = normalizeToPlusMinus180degrees(roll);
166 
167             // Range check the FOV values to ensure they are postive and less than 180degrees
168             if (hfov > 179.0f) {
169                 printf("Pathological horizontal field of view %f clamped to 179 degrees\n", hfov);
170                 hfov = 179.0f;
171             }
172             if (hfov < 1.0f) {
173                 printf("Pathological horizontal field of view %f clamped to 1 degree\n", hfov);
174                 hfov = 1.0f;
175             }
176             if (vfov > 179.0f) {
177                 printf("Pathological horizontal field of view %f clamped to 179 degrees\n", vfov);
178                 vfov = 179.0f;
179             }
180             if (vfov < 1.0f) {
181                 printf("Pathological horizontal field of view %f clamped to 1 degree\n", vfov);
182                 vfov = 1.0f;
183             }
184 
185             // Store the camera info (converting degrees to radians in the process)
186             CameraInfo info;
187             info.position[0] = node.get("x", 0).asFloat();
188             info.position[1] = node.get("y", 0).asFloat();
189             info.position[2] = node.get("z", 0).asFloat();
190             info.yaw         = yaw   * kDegreesToRadians;
191             info.pitch       = pitch * kDegreesToRadians;
192             info.roll        = roll  * kDegreesToRadians;
193             info.hfov        = hfov  * kDegreesToRadians;
194             info.vfov        = vfov  * kDegreesToRadians;
195             info.hflip       = hflip;
196             info.vflip       = vflip;
197             info.cameraId    = cameraId;
198             info.function    = function;
199 
200             mCameras.emplace_back(info);
201         }
202     }
203 
204     // If we got this far, we were successful as long as we found all our child fields
205     return complete;
206 }
207