-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathconditionals.py
More file actions
1328 lines (1196 loc) · 51.3 KB
/
Copy pathconditionals.py
File metadata and controls
1328 lines (1196 loc) · 51.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""
conditionals: "Does task condition Y hold for these objects?"
Task-level API with @atomic/@composite decorators.
Takes `env` as first parameter.
Handles logical modes (all/any/choose) and contact requirements.
All @atomic conditionals support env_id parameter:
env_id=None (default) → returns Tensor(num_envs,) bool (used by IsaacLab TerminationManager)
env_id=<int> → returns bool (used by ConditionalsStateMachine)
"""
from functools import partial
from typing import Literal, Optional, Union
import torch
import robolab.constants
from robolab.core.task.decorators import atomic, composite
from robolab.core.task.predicate_logic import *
from robolab.core.task.predicate_logic import _and, _not, gripper_detached
from robolab.core.task.subtask import Subtask
from robolab.core.world.world_state import get_world
#########################################################
# Composite conditions
#########################################################
@composite
def pick_and_place(
object: str | list[str],
container: str,
logical: Literal["all", "any", "choose"] = "all",
K: Optional[int] = None,
score: float = 1.0
) -> Subtask:
"""
A composite subtask that picks up object(s) and places them in a container.
This function creates parallel subtask sequences for each specified object, where each
object independently progresses through: grab → lift → move → drop → verify placement.
The completion logic determines when the entire group is considered complete.
Args:
object: Single object name or list of object names to manipulate in parallel
container: Target container name where objects should be placed
logical: Completion mode determining when this subtask group succeeds:
- "all": All objects must complete their subtasks (default)
- "any": Success when any single object completes
- "choose": Success when exactly k objects complete (requires k parameter)
k: Number of objects that must complete when logical="choose"
score: Overall score weight for this subtask group (default: 1.0)
"""
if isinstance(object, str):
objects = [object]
else:
objects = list(object)
conditions = {}
for obj in objects:
conditions[obj] = [
(partial(object_grabbed, object=obj), 0.0),
(partial(object_in_container, object=obj, container=container, require_contact_with=False, require_gripper_detached=True), score),
]
return Subtask(name="pick_and_place", conditions=conditions, logical=logical, score=score, K=K)
@composite
def pick_and_place_grouped(
groups: list[dict],
logical: Literal["all", "any", "choose"] = "all",
K: Optional[int] = None,
score: float = 1.0,
) -> Subtask:
"""
A composite subtask where DIFFERENT objects go to DIFFERENT
containers, all tracked in parallel within a single Subtask.
Use when a phase has multiple destinations and within-phase order
doesn't matter — e.g. a swap task where each object's final
destination is fixed but the policy is free to use any maneuver
(direct, table-buffered, interleaved). Encoding the phase as
multiple sequential ``pick_and_place(..., container=X)`` subtasks
instead would hard-code one specific maneuver and produce a
non-monotone score curve under any other policy strategy.
Each ``groups`` entry is a dict ``{"object": <str | list>,
"container": <str>}``. Each named object becomes one parallel
ladder in the resulting Subtask, with its terminal condition
pointing at the group's container. All objects across all groups
track simultaneously; ``logical`` aggregates over the per-object
completions just as for ``pick_and_place``.
Args:
groups: List of group dicts. Each ``{"object": str | list[str],
"container": str}`` defines one or more objects bound to a
single container.
logical: Completion mode — same semantics as ``pick_and_place``.
``"all"`` (default) requires every object across every group
to complete its ladder. ``"any"`` succeeds when any single
object's ladder completes. ``"choose"`` requires exactly K
object-ladders complete.
K: Required for ``logical="choose"``.
score: Overall score weight for this Subtask.
Returns:
Subtask: with one parallel per-object ladder per (object,
container) pair across all groups.
Example:
# Each fruit → bowl, each can → bin; any order across all four
pick_and_place_grouped(
groups=[
{"object": ["lemon_02", "lime01"], "container": "bowl"},
{"object": ["tuna_can", "corn_can"], "container": "bin_a01"},
],
logical="all",
score=1.0,
)
Note:
Use this for subtask tracking only, not for termination
conditions. For terminations,
``object_groups_in_containers`` consumes the same shape.
"""
conditions: dict = {}
for grp in groups:
cont = grp["container"]
objs = grp["object"]
if isinstance(objs, str):
objs = [objs]
for obj_name in objs:
conditions[obj_name] = [
(partial(object_grabbed, object=obj_name), 0.25),
(partial(object_above_bottom, object=obj_name,
reference_object=cont), 0.25),
(partial(object_dropped, object=obj_name), 0.25),
(partial(object_in_container, object=obj_name,
container=cont, tolerance=0.01), 0.25),
]
return Subtask(
name="pick_and_place_grouped",
conditions=conditions,
logical=logical,
score=score,
K=K,
)
@composite
def pick_and_place_on_surface(
object: str | list[str],
surface: str,
logical: Literal["all", "any", "choose"] = "all",
K: Optional[int] = None,
score: float = 1.0
) -> Subtask:
"""
A composite subtask that picks up object(s) and places them on a surface.
Similar to pick_and_place, but verifies stable support on a flat surface using
contact force cone checking rather than containment checks.
Args:
object: Single object name or list of object names to manipulate in parallel
surface: Target surface name where objects should be placed
logical: Completion mode - "all", "any", or "choose"
k: Number of objects that must complete when logical="choose"
score: Overall score weight for this subtask group (default: 1.0)
"""
if isinstance(object, str):
objects = [object]
else:
objects = list(object)
conditions = {}
for obj in objects:
conditions[obj] = [
(partial(object_grabbed, object=obj), 0.0),
(partial(object_on_top, object=obj, reference_object=surface, require_gripper_detached=True), score),
]
return Subtask(name="pick_and_place_on_surface", conditions=conditions, logical=logical, score=score, K=K)
#########################################################
# Atomic conditions - Contact
#########################################################
@atomic
def object_in_contact(
env,
object1: str | list[str],
object2: str | list[str],
logical: str = "any",
K: int = 1,
env_id: int | None = None,
):
"""Checks contact between objects according to logical."""
if logical not in ["any", "all", "choose"]:
raise ValueError(f"Invalid logical: {logical}")
world = get_world(env)
result = in_contact(world, object1, object2, force_threshold=0.1, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_in_contact: {object1} and {object2} in contact (logical={logical}) -> {result}")
return result
@atomic
def object_grabbed(
env,
object: str,
gripper_name: str | list[str] = "gripper",
env_id: int | None = None,
):
"""Check if an object is currently being grabbed by the gripper (in contact with gripper)."""
world = get_world(env)
result = in_contact(world, object, gripper_name, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_grabbed: '{object}' in contact with '{gripper_name}' -> {result}")
return result
@atomic
def object_dropped(
env,
object: str,
gripper_name: str | list[str] = "gripper",
env_id: int | None = None,
):
"""Check if an object has been dropped (in contact with none of the given grippers)."""
world = get_world(env)
result = gripper_detached(world, object, gripper_name, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_dropped: '{object}' not in contact with '{gripper_name}' -> {result}")
return result
@atomic
def object_picked_up(
env,
object: str,
surface: str,
distance: float = 0.05,
env_id: int | None = None,
):
"""Check if object is grabbed and lifted at least `distance` above the surface."""
result = _and(
object_grabbed(env, object, env_id=env_id),
object_above(env, object=object, reference_object=surface, env_id=env_id, z_margin=distance)
)
if robolab.constants.DEBUG:
print(f"object_picked_up: '{object}' grabbed and lifted {distance}m above '{surface}' -> {result}")
return result
#########################################################
# Unified Spatial Conditions (New API)
#########################################################
#
# Parameters:
# require_contact_with: Contact requirement
# - False: no contact check (default)
# - True: must be in contact with reference_object
# - str/list[str]: must be in contact with specified object(s)
# require_gripper_detached: If True, object must NOT be held by gripper
#
@atomic
def object_in_container(
env,
object: str | list[str],
container: str,
tolerance: float = 0.01,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
require_stationary: bool = False,
stationary_threshold: float = 0.05,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are in an open-top container.
Geometric check: the object's centroid is transformed into the container's local
frame and bounds-checked against the container's local AABB (with one
container-height of open-top slack along the container's local +z). Because the
check is performed in the container's coordinates, the predicate is invariant to
container orientation — a flipped or tipped container correctly fails.
"""
def condition(world, obj, env_id=None):
result = in_opentop_container(
world, obj, container,
tolerance=tolerance,
env_id=env_id,
)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, container, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
if require_stationary:
result = _and(result, stationary(world, obj, linear_threshold=stationary_threshold, check_angular=False, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_in_container: {object} in '{container}' (tol={tolerance}, logical={logical}) -> {result}")
return result
@atomic
def object_on_top(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
require_contact_with: Union[str, list[str]] = None,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are stably supported on the top surface of reference_object.
The check is the AND of:
- is_supported_on_surface: contact-force from surface on obj is non-trivial,
upward, and within a 45° cone of vertical.
- centroid_in_footprint: obj's centroid xy lies within the surface's AABB
(with ``tolerance`` slack). z is intentionally not bounded — concave
surfaces (plates with wells, tilted/overhanging objects) make any
all-corners-above-top rule too brittle.
"""
def condition(world, obj, env_id=None):
result = world.is_supported_on_surface(obj, reference_object, env_id=env_id)
result = _and(result, centroid_in_footprint(
world, obj, reference_object, tolerance=tolerance, env_id=env_id
))
if require_contact_with is not None:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_on_top: {object} on top of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_on_bottom(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
z_margin: float = 0.0,
mode: str = "bbox",
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are positioned above the bottom surface of reference_object."""
def condition(world, obj, env_id=None):
result = above_bottom(world, obj, reference_object, tolerance, z_margin, mode, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_on_bottom: {object} above bottom of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_on_center(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are centered on reference_object (XY alignment)."""
def condition(world, obj, env_id=None):
result = center_of(world, obj, reference_object, tolerance, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_on_center: {object} centered on '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_left_of(
env,
object: str | list[str],
reference_object: str,
frame_of_reference: str = "robot",
mirrored: bool = False,
cone_deg: int = 45,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are to the left of reference_object."""
def condition(world, obj, env_id=None):
result = left_of(world, obj, reference_object, frame_of_reference, mirrored, cone_deg, env_id=env_id)
if not require_contact_with and not require_gripper_detached:
result = _and(result, level(world, obj, reference_object, env_id=env_id))
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_left_of: {object} left of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_right_of(
env,
object: str | list[str],
reference_object: str,
frame_of_reference: str = "robot",
mirrored: bool = False,
cone_deg: int = 45,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are to the right of reference_object."""
def condition(world, obj, env_id=None):
result = right_of(world, obj, reference_object, frame_of_reference, mirrored, cone_deg, env_id=env_id)
if not require_contact_with and not require_gripper_detached:
result = _and(result, level(world, obj, reference_object, env_id=env_id))
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_right_of: {object} right of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_in_front_of(
env,
object: str | list[str],
reference_object: str,
frame_of_reference: str = "robot",
mirrored: bool = False,
cone_deg: int = 45,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are in front of reference_object."""
def condition(world, obj, env_id=None):
result = in_front_of(world, obj, reference_object, frame_of_reference, mirrored, cone_deg, env_id=env_id)
if not require_contact_with and not require_gripper_detached:
result = _and(result, level(world, obj, reference_object, env_id=env_id))
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_in_front_of: {object} in front of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_behind(
env,
object: str | list[str],
reference_object: str,
frame_of_reference: str = "robot",
mirrored: bool = False,
cone_deg: int = 45,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are behind reference_object."""
def condition(world, obj, env_id=None):
result = behind(world, obj, reference_object, frame_of_reference, mirrored, cone_deg, env_id=env_id)
if not require_contact_with and not require_gripper_detached:
result = _and(result, level(world, obj, reference_object, env_id=env_id))
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_behind: {object} behind '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_next_to(
env,
object: str | list[str],
reference_object: str,
dist: float = 0.05,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are within a certain distance of reference_object."""
def condition(world, obj, env_id=None):
result = next_to(world, obj, reference_object, dist, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_next_to: {object} within {dist}m of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_below_top(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
z_margin: float = 0.0,
mode: str = "bbox",
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are below the top surface of reference_object."""
def condition(world, obj, env_id=None):
result = below_top(world, obj, reference_object, tolerance, z_margin, mode, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_below_top: {object} below top of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_below(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
z_margin: float = 0.0,
mode: str = "bbox",
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are below the bottom surface of reference_object."""
def condition(world, obj, env_id=None):
result = below_bottom(world, obj, reference_object, tolerance, z_margin, mode, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_below: {object} below bottom of '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_enclosed(
env,
object: str | list[str],
container: str,
tolerance: float = 0.01,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects' bounding boxes are fully enclosed inside the container."""
def condition(world, obj, env_id=None):
result = enclosed(world, obj, container, tolerance, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, container, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_enclosed: {object} enclosed in '{container}' (logical={logical}) -> {result}")
return result
@atomic
def object_inside(
env,
object: str | list[str],
container: str,
tolerance: float = 0.01,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects' centroids are inside the container's bounding box."""
def condition(world, obj, env_id=None):
result = inside(world, obj, container, tolerance, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, container, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_inside: {object} inside '{container}' (logical={logical}) -> {result}")
return result
@atomic
def object_outside_of(
env,
object: str | list[str],
container: str,
tolerance: float = 0.01,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are outside the container.
Symmetric with ``object_in_container``: the object is "outside" iff fewer
than half of its hull vertices fall in the container's open-top hull
(``frac_inside < 0.5``). Equivalent to ``not in_opentop_container``.
"""
def condition(world, obj, env_id=None):
result = _not(in_opentop_container(world, obj, container, tolerance, env_id=env_id))
if require_contact_with is True:
result = _and(result, in_contact(world, obj, container, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_outside_of: {object} outside '{container}' (logical={logical}) -> {result}")
return result
@atomic
def object_upright(
env,
object: str | list[str],
tolerance: float = 0.1,
up_axis: str = "z",
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are standing upright (oriented correctly)."""
def condition(world, obj, env_id=None):
result = upright(world, obj, tolerance, up_axis, env_id=env_id)
if require_contact_with is True:
raise ValueError(
"object_upright(require_contact_with=True) is invalid: object_upright "
"has no reference_object. Pass a body name (str) or list of body names instead."
)
if require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_upright: {object} upright (up_axis={up_axis}, logical={logical}) -> {result}")
return result
@atomic
def object_at(
env,
object: str | list[str],
position: tuple[float, float, float],
tolerance: float = 0.02,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Check if objects are at a specific 3D position within tolerance."""
if logical not in ["any", "all", "choose"]:
raise ValueError(f"Invalid logical: {logical}")
world = get_world(env)
object_list = [object] if isinstance(object, str) else list(object)
pos_target = torch.tensor(position, dtype=torch.float32, device=world.env.device)
def check_obj(world, obj, env_id=None):
pos, _ = world.get_pose(obj, env_id=env_id)
if env_id is not None:
at_pos = torch.allclose(pos, pos_target, atol=tolerance)
if require_gripper_detached:
at_pos = at_pos and not in_contact(world, obj, gripper_name, env_id=env_id)
return at_pos
else:
# pos: (N, 3)
diff = torch.abs(pos - pos_target.unsqueeze(0))
at_pos = (diff <= tolerance).all(dim=1) # (N,)
if require_gripper_detached:
at_pos = at_pos & gripper_detached(world, obj, gripper_name, env_id=None)
return at_pos
result = evaluate_spatial_condition(env, object, check_obj, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_at: {object} at {position} (tol={tolerance}, logical={logical}) -> {result}")
return result
@atomic
def object_between(
env,
object: str | list[str],
reference_obj1: str,
reference_obj2: str,
check_alignment: bool = True,
alignment_tolerance: float = 0.1,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are positioned between two reference objects."""
def condition(world, obj, env_id=None):
result = between(world, obj, reference_obj1, reference_obj2, check_alignment, alignment_tolerance, env_id=env_id)
if require_contact_with and require_contact_with is not True:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_between: {object} between '{reference_obj1}' and '{reference_obj2}' (logical={logical}) -> {result}")
return result
@atomic
def objects_in_line(
env,
objects: list[str],
axis: str | None = None,
tolerance: float = 0.05,
min_spacing: float = 0.02,
env_id: int | None = None,
):
"""Checks if multiple objects are arranged in a line/row."""
world = get_world(env)
result = in_line(world, objects, axis, tolerance, min_spacing, env_id=env_id)
if robolab.constants.DEBUG:
print(f"objects_in_line: {objects} in a line (axis={axis}) -> {result}")
return result
@atomic
def objects_stationary(
env,
object: str | list[str],
linear_threshold: float = 0.01,
angular_threshold: float = 0.1,
check_angular: bool = True,
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects have stopped moving (velocity near zero)."""
result = evaluate_spatial_condition(
env, object,
lambda world, obj, env_id=None: stationary(world, obj, linear_threshold, angular_threshold, check_angular, env_id=env_id),
logical, K, env_id=env_id
)
if robolab.constants.DEBUG:
print(f"objects_stationary: {object} stationary (logical={logical}) -> {result}")
return result
@atomic
def object_center_of(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Check if the geometric centers of objects are aligned with reference_object (XY plane only)."""
def condition(world, obj, env_id=None):
result = center_of(world, obj, reference_object, tolerance, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_center_of: {object} centered on '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_above(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
z_margin: float = 0.0,
mode: str = "bbox",
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Check if objects are geometrically positioned above the top surface of reference_object."""
def condition(world, obj, env_id=None):
result = above_top(world, obj, reference_object, tolerance, z_margin, mode, env_id=env_id)
if require_contact_with is True:
result = _and(result, in_contact(world, obj, reference_object, env_id=env_id))
elif require_contact_with:
result = _and(result, in_contact(world, obj, require_contact_with, env_id=env_id))
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_above: {object} above '{reference_object}' (logical={logical}) -> {result}")
return result
@atomic
def object_above_bottom(
env,
object: str | list[str],
reference_object: str,
tolerance: float = 0.01,
z_margin: float = 0.0,
mode: str = "bbox",
require_contact_with: Union[bool, str, list[str]] = False,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Check if objects are positioned above the bottom surface of reference_object."""
return object_on_bottom(
env, object, reference_object, tolerance, z_margin, mode,
require_contact_with, require_gripper_detached, gripper_name,
logical, K, env_id
)
#########################################################
# Special compound conditions
#########################################################
@atomic
def object_outside_of_and_on_surface(
env,
object: str | list[str],
container: str,
surface: str,
tolerance: float = 0.01,
require_gripper_detached: bool = False,
gripper_name: str | list[str] = "gripper",
logical: str = "all",
K: int = 1,
env_id: int | None = None,
):
"""Checks if objects are outside of a container AND stably supported on a surface.
Symmetric with ``object_in_container``: ``not in_opentop_container``
(frac_inside < 0.5) for the container check; surface support unchanged.
"""
def condition(world, obj, env_id=None):
result = _and(
_not(in_opentop_container(world, obj, container, tolerance, env_id=env_id)),
world.is_supported_on_surface(obj, surface, env_id=env_id)
)
if require_gripper_detached:
result = _and(result, gripper_detached(world, obj, gripper_name, env_id=env_id))
return result
result = evaluate_spatial_condition(env, object, condition, logical, K, env_id=env_id)
if robolab.constants.DEBUG:
print(f"object_outside_of_and_on_surface: {object} outside '{container}' and on '{surface}' (logical={logical}) -> {result}")
return result
@atomic
def object_groups_in_containers(
env,
groups: list[dict],
env_id: int | None = None,
):
"""Checks multiple (object(s), container) groups; returns True only if all groups satisfy placement."""
if groups is None or len(groups) == 0:
if env_id is not None:
return False
return torch.zeros(env.num_envs, dtype=torch.bool, device=env.device)
results = []
for group in groups:
objects = group.get("object", [])