NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Building.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24#define _NETSIM_MOBILITY_CODE_
25#include "main.h"
26#include "Mobility.h"
27#include "MobilityInterface.h"
28
29typedef struct stru_netsim_office
30{
31 UINT id;
32 char* name;
33 double X1;
34 double X2;
35 double Y1;
36 double Y2;
37}NETSIM_OFFICE, * ptrNETSIM_OFFICE;
38static UINT officeCount = 0;
39static ptrNETSIM_OFFICE* officeList;
40
41#define MOBILITY_OFFICE_COUNT_DEFAULT 0
42#define MOBILITY_OFFICE_ID_DEFAULT 0
43#define MOBILITY_OFFICE_NAME_DEFAULT _strdup("Office1")
44#define MOBILITY_OFFICE_X1_DEFAULT 0
45#define MOBILITY_OFFICE_Y1_DEFAULT 0
46#define MOBILITY_OFFICE_X2_DEFAULT 0
47#define MOBILITY_OFFICE_Y2_DEFAULT 0
48
49static void configure_office(void* xmlNode, ptrNETSIM_OFFICE office)
50{
51 getXmlVar(&office->id, ID, xmlNode, 1, _UINT, MOBILITY_OFFICE);
52 getXmlVar(&office->name, NAME, xmlNode, 1, _STRING, MOBILITY_OFFICE);
53 getXmlVar(&office->X1, X1, xmlNode, 1, _DOUBLE, MOBILITY_OFFICE);
54 getXmlVar(&office->X2, X2, xmlNode, 1, _DOUBLE, MOBILITY_OFFICE);
55 getXmlVar(&office->Y1, Y1, xmlNode, 1, _DOUBLE, MOBILITY_OFFICE);
56 getXmlVar(&office->Y2, Y2, xmlNode, 1, _DOUBLE, MOBILITY_OFFICE);
57}
58
59void fn_NetSim_MObility_configureOffice(void* xmlNetSimNode)
60{
61 getXmlVar(&officeCount, OFFICE_COUNT, xmlNetSimNode, 1, _UINT, MOBILITY);
62 if (officeCount)
63 officeList = calloc(officeCount, sizeof * officeList);
64 else
65 return;
66
67 UINT i;
68 for (i = 0; i < officeCount; i++)
69 {
70 void* xmlChild = fn_NetSim_xmlGetChildElement(xmlNetSimNode, "OFFICE", i);
71 if (!xmlChild)
72 break;
73
74 officeList[i] = calloc(1, sizeof * officeList[i]);
75 configure_office(xmlChild, officeList[i]);
76 }
77
78}
79
80_declspec(dllexport) bool fnMobility_isPosInsideBuilding(NetSim_COORDINATES* pos, NETSIM_ID id)
81{
82 if ((pos->X >= officeList[id - 1]->X1 && pos->X <= officeList[id - 1]->X2) &&
83 (pos->Y >= officeList[id - 1]->Y1 && pos->Y <= officeList[id - 1]->Y2))
84 return true;
85 return false;
86}
87
88_declspec(dllexport) UINT fnMobility_isPosInsideAnyBuilding(NetSim_COORDINATES* pos)
89{
90 UINT i;
91 for (i = 0; i < officeCount; i++)
92 {
93 if (fnMobility_isPosInsideBuilding(pos, i + 1))
94 return i + 1;
95 }
96 return 0;
97}
98
99_declspec(dllexport) bool fnMobility_findIntersectionPointbyBuilding(NetSim_COORDINATES* p1, NetSim_COORDINATES* p2,
100 UINT id, NetSim_COORDINATES* ret)
101{
102 NetSim_COORDINATES of[4];
103
104 int i;
105 for (i = 0; i < 4; i++)
106 memset(&of[i], 0, sizeof of[i]);
107
108 of[0].X = officeList[id - 1]->X1;
109 of[0].Y = officeList[id - 1]->Y1;
110
111 of[1].X = officeList[id - 1]->X1;
112 of[1].Y = officeList[id - 1]->Y2;
113
114 of[2].X = officeList[id - 1]->X2;
115 of[2].Y = officeList[id - 1]->Y2;
116
117 of[3].X = officeList[id - 1]->X2;
118 of[3].Y = officeList[id - 1]->Y1;
119
120 if (fnMobility_findIntersect(p1, p2, &of[0], &of[1], ret))
121 return true;
122
123 if (fnMobility_findIntersect(p1, p2, &of[1], &of[2], ret))
124 return true;
125
126 if (fnMobility_findIntersect(p1, p2, &of[2], &of[3], ret))
127 return true;
128
129 if (fnMobility_findIntersect(p1, p2, &of[3], &of[0], ret))
130 return true;
131
132 return false;
133}
134
135_declspec(dllexport) double fnMobility_findIndoorDistance(NetSim_COORDINATES* p1, NetSim_COORDINATES* p2)
136{
137 UINT id1, id2, id;
138 NetSim_COORDINATES* indoor;
139 NetSim_COORDINATES* outdoor;
140
141 id1 = fnMobility_isPosInsideAnyBuilding(p1);
142 id2 = fnMobility_isPosInsideAnyBuilding(p2);
143
144 if (id1 == 0 && id2 == 0)
145 return 0.0; // Both pos are outdoor
146
147 if (id1 > 0 && id2 > 0)
148 return fn_NetSim_Utilities_CalculateDistance(p1, p2); //Both are indoor
149
150 if (id1 > 0)
151 {
152 indoor = p1;
153 outdoor = p2;
154 id = id1;
155 }
156 else
157 {
158 indoor = p2;
159 outdoor = p1;
160 id = id2;
161 }
162
163 NetSim_COORDINATES ret;
164 memset(&ret, 0, sizeof ret);
165 fnMobility_findIntersectionPointbyBuilding(p1, p2, id, &ret);
166
167 if (id1 > 0)
168 return fn_NetSim_Utilities_CalculateDistance(p1, &ret);
169 else
170 return fn_NetSim_Utilities_CalculateDistance(p2, &ret);
171}