Skip to content

Commit 003e74f

Browse files
Axel1092doterop
andauthored
* Added raycasting and point projection capabilities to python api. * Added documentation of new functions. * Fixed missing .f on floats Co-authored-by: doterop <[email protected]>
1 parent 52d7060 commit 003e74f

File tree

18 files changed

+533
-209
lines changed

18 files changed

+533
-209
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TrafficManager/build
66
Util/Build
77
Install
88
Plugins
9+
!Unreal/CarlaUE4/Plugins
910

1011
/ExportedMaps
1112
/Import/*

Docs/python_api.md

+232-202
Large diffs are not rendered by default.

LibCarla/source/carla/client/World.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,25 @@ namespace client {
176176
_episode.Lock()->EnableEnvironmentObjects(env_objects_ids, enable);
177177
}
178178

179+
boost::optional<rpc::LabelledPoint> World::ProjectPoint(
180+
geom::Location location, geom::Vector3D direction, float search_distance) const {
181+
auto result = _episode.Lock()->ProjectPoint(location, direction, search_distance);
182+
if (result.first) {
183+
return result.second;
184+
}
185+
return {};
186+
}
187+
188+
boost::optional<rpc::LabelledPoint> World::GroundProjection(
189+
geom::Location location, float search_distance) const {
190+
const geom::Vector3D DownVector(0,0,-1);
191+
return ProjectPoint(location, DownVector, search_distance);
192+
}
193+
194+
std::vector<rpc::LabelledPoint> World::CastRay(
195+
geom::Location start_location, geom::Location end_location) const {
196+
return _episode.Lock()->CastRay(start_location, end_location);
197+
}
198+
179199
} // namespace client
180200
} // namespace carla

LibCarla/source/carla/client/World.h

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "carla/rpc/AttachmentType.h"
2020
#include "carla/rpc/EpisodeSettings.h"
2121
#include "carla/rpc/EnvironmentObject.h"
22+
#include "carla/rpc/LabelledPoint.h"
2223
#include "carla/rpc/VehiclePhysicsControl.h"
2324
#include "carla/rpc/WeatherParameters.h"
2425
#include "carla/rpc/VehicleLightStateList.h"
@@ -162,6 +163,15 @@ namespace client {
162163
std::vector<uint64_t> env_objects_ids,
163164
bool enable) const;
164165

166+
boost::optional<rpc::LabelledPoint> ProjectPoint(
167+
geom::Location location, geom::Vector3D direction, float search_distance = 10000.f) const;
168+
169+
boost::optional<rpc::LabelledPoint> GroundProjection(
170+
geom::Location location, float search_distance = 10000.0) const;
171+
172+
std::vector<rpc::LabelledPoint> CastRay(
173+
geom::Location start_location, geom::Location end_location) const;
174+
165175
private:
166176

167177
detail::EpisodeProxy _episode;

LibCarla/source/carla/client/detail/Client.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,18 @@ namespace detail {
461461
_pimpl->AsyncCall("enable_environment_objects", std::move(env_objects_ids), enable);
462462
}
463463

464+
std::pair<bool,rpc::LabelledPoint> Client::ProjectPoint(
465+
geom::Location location, geom::Vector3D direction, float search_distance) const {
466+
using return_t = std::pair<bool,rpc::LabelledPoint>;
467+
return _pimpl->CallAndWait<return_t>("project_point", location, direction, search_distance);
468+
}
469+
470+
std::vector<rpc::LabelledPoint> Client::CastRay(
471+
geom::Location start_location, geom::Location end_location) const {
472+
using return_t = std::vector<rpc::LabelledPoint>;
473+
return _pimpl->CallAndWait<return_t>("cast_ray", start_location, end_location);
474+
}
475+
464476
} // namespace detail
465477
} // namespace client
466478
} // namespace carla

LibCarla/source/carla/client/detail/Client.h

+8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#include "carla/NonCopyable.h"
1111
#include "carla/Time.h"
1212
#include "carla/geom/Transform.h"
13+
#include "carla/geom/Location.h"
1314
#include "carla/rpc/Actor.h"
1415
#include "carla/rpc/ActorDefinition.h"
1516
#include "carla/rpc/AttachmentType.h"
1617
#include "carla/rpc/Command.h"
1718
#include "carla/rpc/CommandResponse.h"
1819
#include "carla/rpc/EpisodeInfo.h"
1920
#include "carla/rpc/EpisodeSettings.h"
21+
#include "carla/rpc/LabelledPoint.h"
2022
#include "carla/rpc/LightState.h"
2123
#include "carla/rpc/MapInfo.h"
2224
#include "carla/rpc/EnvironmentObject.h"
@@ -297,6 +299,12 @@ namespace detail {
297299
std::vector<uint64_t> env_objects_ids,
298300
bool enable) const;
299301

302+
std::pair<bool,rpc::LabelledPoint> ProjectPoint(
303+
geom::Location location, geom::Vector3D direction, float search_distance) const;
304+
305+
std::vector<rpc::LabelledPoint> CastRay(
306+
geom::Location start_location, geom::Location end_location) const;
307+
300308
private:
301309

302310
class Pimpl;

LibCarla/source/carla/client/detail/Simulator.h

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "carla/profiler/LifetimeProfiled.h"
2424
#include "carla/rpc/TrafficLightState.h"
2525
#include "carla/rpc/VehicleLightStateList.h"
26+
#include "carla/rpc/LabelledPoint.h"
2627

2728
#include <boost/optional.hpp>
2829

@@ -241,6 +242,16 @@ namespace detail {
241242
_client.EnableEnvironmentObjects(env_objects_ids, enable);
242243
}
243244

245+
std::pair<bool,rpc::LabelledPoint> ProjectPoint(
246+
geom::Location location, geom::Vector3D direction, float search_distance) const {
247+
return _client.ProjectPoint(location, direction, search_distance);
248+
}
249+
250+
std::vector<rpc::LabelledPoint> CastRay(
251+
geom::Location start_location, geom::Location end_location) const {
252+
return _client.CastRay(start_location, end_location);
253+
}
254+
244255
/// @}
245256
// =========================================================================
246257
/// @name AI
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2+
// de Barcelona (UAB).
3+
//
4+
// This work is licensed under the terms of the MIT license.
5+
// For a copy, see <https://opensource.org/licenses/MIT>.
6+
7+
#pragma once
8+
9+
#include "carla/MsgPack.h"
10+
#include "carla/rpc/Location.h"
11+
#include "carla/rpc/ObjectLabel.h"
12+
13+
namespace carla {
14+
namespace rpc {
15+
16+
struct LabelledPoint {
17+
18+
LabelledPoint () {}
19+
LabelledPoint (Location location, CityObjectLabel label)
20+
: _location(location), _label(label)
21+
{}
22+
23+
Location _location;
24+
25+
CityObjectLabel _label;
26+
27+
MSGPACK_DEFINE_ARRAY(_location, _label);
28+
29+
};
30+
31+
}
32+
}

LibCarla/source/carla/rpc/ObjectLabel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ namespace rpc {
2323
RoadLines = 6u,
2424
Roads = 7u,
2525
Sidewalks = 8u,
26-
TrafficSigns = 12u,
2726
Vegetation = 9u,
2827
Vehicles = 10u,
2928
Walls = 11u,
29+
TrafficSigns = 12u,
3030
Sky = 13u,
3131
Ground = 14u,
3232
Bridge = 15u,

PythonAPI/carla/source/libcarla/World.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ void export_world() {
201201
.value("Terrain", cr::CityObjectLabel::Terrain)
202202
;
203203

204+
class_<cr::LabelledPoint>("LabelledPoint", no_init)
205+
.def_readonly("location", &cr::LabelledPoint::_location)
206+
.def_readonly("label", &cr::LabelledPoint::_label)
207+
;
208+
204209
#define SPAWN_ACTOR_WITHOUT_GIL(fn) +[]( \
205210
cc::World &self, \
206211
const cc::ActorBlueprint &blueprint, \
@@ -247,6 +252,9 @@ void export_world() {
247252
.def("get_level_bbs", &GetLevelBBs, (arg("actor_type")=cr::CityObjectLabel::None))
248253
.def("get_environment_objects", &GetEnvironmentObjects)
249254
.def("enable_environment_objects", &EnableEnvironmentObjects, (arg("env_objects_ids"), arg("enable")))
255+
.def("cast_ray", CALL_RETURNING_LIST_2(cc::World, CastRay, cg::Location, cg::Location), (arg("initial_location"), arg("final_location")))
256+
.def("project_point", CALL_RETURNING_OPTIONAL_3(cc::World, ProjectPoint, cg::Location, cg::Vector3D, float), (arg("location"), arg("direction"), arg("search_distance")=10000.f))
257+
.def("ground_projection", CALL_RETURNING_OPTIONAL_2(cc::World, GroundProjection, cg::Location, float), (arg("location"), arg("search_distance")=10000.f))
250258
.def(self_ns::str(self_ns::self))
251259
;
252260

PythonAPI/carla/source/libcarla/libcarla.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ static boost::python::object OptionalToPythonObject(OptionalT &optional) {
111111
return OptionalToPythonObject(optional); \
112112
}
113113

114+
#define CALL_RETURNING_OPTIONAL_2(cls, fn, T1_, T2_) +[](const cls &self, T1_ t1, T2_ t2) { \
115+
auto optional = self.fn(std::forward<T1_>(t1), std::forward<T2_>(t2)); \
116+
return OptionalToPythonObject(optional); \
117+
}
118+
119+
#define CALL_RETURNING_OPTIONAL_3(cls, fn, T1_, T2_, T3_) +[](const cls &self, T1_ t1, T2_ t2, T3_ t3) { \
120+
auto optional = self.fn(std::forward<T1_>(t1), std::forward<T2_>(t2), std::forward<T3_>(t3)); \
121+
return OptionalToPythonObject(optional); \
122+
}
123+
114124
#define CALL_RETURNING_OPTIONAL_WITHOUT_GIL(cls, fn) +[](const cls &self) { \
115125
carla::PythonUtil::ReleaseGIL unlock; \
116126
auto optional = self.fn(); \

PythonAPI/docs/world.yml

+62-2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@
186186
An attachment that expands or retracts the position of the actor, depending on its parent. This attachment is only recommended to record videos from the simulation where a smooth movement is needed. SpringArms are an Unreal Engine component so [check the UE docs](https://docs.unrealengine.com/en-US/Gameplay/HowTo/UsingCameras/SpringArmComponents/index.html) to learn more about them. <br><b style="color:red;">Warning:</b> The <b>SpringArm</b> attachment presents weird behaviors when an actor is spawned with a relative translation in the Z-axis (e.g. <code>child_location = Location(0,0,2)</code>).
187187
# --------------------------------------
188188

189+
- class_name: LabelledPoint
190+
# - DESCRIPTION ------------------------
191+
doc: >
192+
Class that represent a position in space with a semantic label.
193+
# - PROPERTIES -------------------------
194+
instance_variables:
195+
- var_name: location
196+
doc: >
197+
Position in 3D space.
198+
- var_name: label
199+
doc: >
200+
Semantic tag of the point.
201+
# --------------------------------------
202+
189203
- class_name: World
190204
# - DESCRIPTION ------------------------
191205
doc: >
@@ -421,13 +435,59 @@
421435
New conditions to be applied.
422436
doc: >
423437
Changes the weather parameteres ruling the simulation to another ones defined in an object.
424-
# --------------------------------------
438+
# --------------------------------------
439+
- def_name: cast_ray
440+
return: list(carla.LabelledPoint)
441+
params:
442+
- param_name: initial_location
443+
type: carla.Location
444+
doc: >
445+
The initial position of the ray.
446+
- param_name: final_location
447+
type: carla.Location
448+
doc: >
449+
The final position of the ray.
450+
doc: >
451+
Casts a ray from the specified initial_location to final_location. The function then detects all geometries intersecting the ray and returns a list of carla.LabelledPoint in order.
452+
# --------------------------------------
453+
- def_name: project_point
454+
return: carla.LabelledPoint
455+
params:
456+
- param_name: location
457+
type: carla.Location
458+
doc: >
459+
The point to be projected.
460+
- param_name: direction
461+
type: carla.Vector3D
462+
doc: >
463+
The direction of projection.
464+
- param_name: search_distance
465+
type: float
466+
doc: >
467+
The maximum distance to perform the projection
468+
doc: >
469+
Projects the specified point to the desired direction in the scene. The functions casts a ray from location in a direction and returns a carla.Labelled object with the first geometry this ray intersects. If no geometry is found in the search_distance range the function returns `None`.
470+
# --------------------------------------
471+
- def_name: ground_projection
472+
return: carla.LabelledPoint
473+
params:
474+
- param_name: location
475+
type: carla.Location
476+
doc: >
477+
The point to be projected.
478+
- param_name: search_distance
479+
type: float
480+
doc: >
481+
The maximum distance to perform the projection
482+
doc: >
483+
Projects the specified point downwards in the scene. The functions casts a ray from location in the direction (0,0,-1) (downwards) and returns a carla.Labelled object with the first geometry this ray intersects (usually the ground). If no geometry is found in the search_distance range the function returns `None`.
484+
# --------------------------------------
425485
- def_name: __str__
426486
return:
427487
string
428488
doc: >
429489
The content of the world is parsed and printed as a brief report of its current state.
430-
# --------------------------------------
490+
# --------------------------------------
431491

432492
- class_name: DebugHelper
433493
# - DESCRIPTION ------------------------

Unreal/CarlaUE4/Config/DefaultEngine.ini

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ InitialAverageFrameRate=0.016667
9797
PhysXTreeRebuildRate=10
9898
DefaultBroadphaseSettings=(bUseMBPOnClient=False,bUseMBPOnServer=False,MBPBounds=(Min=(X=0.000000,Y=0.000000,Z=0.000000),Max=(X=0.000000,Y=0.000000,Z=0.000000),IsValid=0),MBPNumSubdivs=2)
9999

100+
[/Script/WindowsTargetPlatform.WindowsTargetSettings]
101+
DefaultGraphicsRHI=DefaultGraphicsRHI_DX12
102+
100103
[/Script/Engine.CollisionProfile]
101104
+Profiles=(Name="CustomSensorCollision",CollisionEnabled=QueryOnly,bCanModify=True,ObjectTypeName="SensorObject",CustomResponses=((Channel="WorldStatic",Response=ECR_Ignore),(Channel="WorldDynamic",Response=ECR_Ignore),(Channel="Pawn",Response=ECR_Ignore),(Channel="Visibility",Response=ECR_Ignore),(Channel="Camera",Response=ECR_Ignore),(Channel="PhysicsBody",Response=ECR_Ignore),(Channel="Vehicle",Response=ECR_Ignore),(Channel="Destructible",Response=ECR_Ignore),(Channel="SensorObject"),(Channel="SensorTrace")),HelpMessage="Used for custom collision meshes for objects that has very complex meshes but we want them to appear in raycast based sensors")
102105
+DefaultChannelResponses=(Channel=ECC_GameTraceChannel1,DefaultResponse=ECR_Ignore,bTraceType=False,bStaticObject=False,Name="SensorObject")
103106
+DefaultChannelResponses=(Channel=ECC_GameTraceChannel2,DefaultResponse=ECR_Ignore,bTraceType=True,bStaticObject=False,Name="SensorTrace")
107+
+DefaultChannelResponses=(Channel=ECC_GameTraceChannel3,DefaultResponse=ECR_Overlap,bTraceType=True,bStaticObject=False,Name="OverlapChannel")
104108
+EditProfiles=(Name="BlockAll",CustomResponses=((Channel="SensorObject"),(Channel="SensorTrace")))
105109
+EditProfiles=(Name="OverlapAll",CustomResponses=((Channel="SensorObject",Response=ECR_Overlap),(Channel="SensorTrace",Response=ECR_Overlap)))
106110

107-
[/Script/WindowsTargetPlatform.WindowsTargetSettings]
108-
DefaultGraphicsRHI=DefaultGraphicsRHI_DX12
109111

Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include "Carla/OpenDrive/OpenDrive.h"
1111
#include "Carla/Util/DebugShapeDrawer.h"
1212
#include "Carla/Util/NavigationMesh.h"
13+
#include "Carla/Util/RayTracer.h"
1314
#include "Carla/Vehicle/CarlaWheeledVehicle.h"
1415
#include "Carla/Walker/WalkerController.h"
1516
#include "GameFramework/CharacterMovementComponent.h"
17+
#include "Carla/Game/Tagger.h"
1618

1719
#include <compiler/disable-ue4-macros.h>
1820
#include <carla/Functional.h>
@@ -25,6 +27,7 @@
2527
#include <carla/rpc/DebugShape.h>
2628
#include <carla/rpc/EpisodeInfo.h>
2729
#include <carla/rpc/EpisodeSettings.h>
30+
#include "carla/rpc/LabelledPoint.h"
2831
#include <carla/rpc/LightState.h>
2932
#include <carla/rpc/MapInfo.h>
3033
#include <carla/rpc/EnvironmentObject.h>
@@ -46,6 +49,7 @@
4649

4750
#include <vector>
4851
#include <map>
52+
#include <tuple>
4953

5054
template <typename T>
5155
using R = carla::rpc::Response<T>;
@@ -1410,6 +1414,28 @@ void FCarlaServer::FPimpl::BindActions()
14101414
};
14111415

14121416

1417+
// ~~ Ray Casting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1418+
1419+
BIND_SYNC(project_point) << [this]
1420+
(cr::Location Location, cr::Vector3D Direction, float SearchDistance)
1421+
-> R<std::pair<bool,cr::LabelledPoint>>
1422+
{
1423+
REQUIRE_CARLA_EPISODE();
1424+
auto *World = Episode->GetWorld();
1425+
constexpr float meter_to_centimeter = 100.0f;
1426+
return URayTracer::ProjectPoint(Location, Direction.ToFVector(),
1427+
meter_to_centimeter * SearchDistance, World);
1428+
};
1429+
1430+
BIND_SYNC(cast_ray) << [this]
1431+
(cr::Location StartLocation, cr::Location EndLocation)
1432+
-> R<std::vector<cr::LabelledPoint>>
1433+
{
1434+
REQUIRE_CARLA_EPISODE();
1435+
auto *World = Episode->GetWorld();
1436+
return URayTracer::CastRay(StartLocation, EndLocation, World);
1437+
};
1438+
14131439
}
14141440

14151441
// =============================================================================

Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
1+
// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
22
// de Barcelona (UAB).
33
//
44
// This work is licensed under the terms of the MIT license.

Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Util/BoundingBoxCalculator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
1+
// Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
22
// de Barcelona (UAB).
33
//
44
// This work is licensed under the terms of the MIT license.

0 commit comments

Comments
 (0)