Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/app/schemas/direct_3d_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ class Direct3DRenderDiagnostics(BaseModel):
source_locked_rlasm_pixel_coverage: float | None = None
scene_lower_context_coverage: float | None = None
minimum_scene_lower_context_coverage: float | None = None
structural_edge_guide_attached: Literal[True] = True
structural_edge_guide_attached: bool = True
finish_fusion: Direct3DFinishFusionDiagnostics | None = None
provider_raw_structural_edge_fidelity: Direct3DRawStructuralEdgeFidelityDiagnostics | None = None
macro_design_fidelity: Direct3DMacroDesignFidelityDiagnostics | None = None
Expand Down
299 changes: 174 additions & 125 deletions backend/app/services/direct_3d_render.py

Large diffs are not rendered by default.

31 changes: 26 additions & 5 deletions backend/app/services/render_fidelity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
)


def presentation_entourage_lock(*, add_people: bool = False, add_vehicles: bool = False) -> str:
def presentation_entourage_lock(
*,
add_people: bool = False,
add_vehicles: bool = False,
preserve_captured_entourage: bool = True,
) -> str:
"""Explicit UI selections override style/custom text; existing context stays intact."""
people = (
"Add a restrained number of realistically scaled people only on existing visible sidewalks, paths or plazas, "
Expand All @@ -43,7 +48,12 @@ def presentation_entourage_lock(*, add_people: bool = False, add_vehicles: bool
+ people
+ " "
+ vehicles
+ " Preserve any people or vehicles already captured in the existing context. "
+ (
" Preserve any people or vehicles already captured in the existing context."
if preserve_captured_entourage
else ""
)
+ " "
"These selections never permit changes to camera, buildings, streets, park layout, paths or furniture."
)

Expand All @@ -56,11 +66,22 @@ def append_render_preservation_lock(prompt: str, *, max_length: int = 32_000) ->
return prompt[: max_length - len(suffix)].rstrip() + suffix


def apply_presentation_selections(prompt: str, *, add_people: bool = False, add_vehicles: bool = False) -> str:
def apply_presentation_selections(
prompt: str,
*,
add_people: bool = False,
add_vehicles: bool = False,
preserve_captured_entourage: bool = True,
reference_instruction: str = "",
) -> str:
suffix = (
"\n"
+ presentation_entourage_lock(add_people=add_people, add_vehicles=add_vehicles)
+ presentation_entourage_lock(
add_people=add_people,
add_vehicles=add_vehicles,
preserve_captured_entourage=preserve_captured_entourage,
)
+ "\n"
+ RENDER_PRESERVATION_LOCK
+ (reference_instruction or RENDER_PRESERVATION_LOCK)
)
return prompt.removesuffix(RENDER_PRESERVATION_LOCK)[: 32_000 - len(suffix)].rstrip() + suffix
115 changes: 81 additions & 34 deletions backend/app/services/site_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@
import base64
import logging
import math
import re
import time
from dataclasses import dataclass

import httpx
from pyproj import Geod

from app.core.config import get_settings

logger = logging.getLogger(__name__)

_CACHE: dict[tuple, tuple[float, dict | None]] = {}
_CACHE_TTL_S = 600
_STREET_VIEW_URL = "https://maps.googleapis.com/maps/api/streetview"
_STREET_VIEW_GEOD = Geod(ellps="WGS84")
_STREET_VIEW_PROBE_DISTANCES_M = (6, 12)
_STREET_VIEW_MAX_CROSS_VIEW_OFFSET_M = 8

# NOTE: Places/tenant TEXT grounding was removed 2026-06-10 — it forced business
# names ("Happy Cake", "1bridge Marketing") into residential scenes where they
Expand All @@ -33,6 +40,70 @@
# drop the textual one. (Re-add later behind a commercial-vs-residential heuristic.)


@dataclass(frozen=True)
class _StreetViewPanorama:
pano_id: str
date: str | None
offset_m: float


async def _streetview_metadata(client: httpx.AsyncClient, lat: float, lng: float, radius_m: int, key: str) -> dict:
try:
response = await client.get(
f"{_STREET_VIEW_URL}/metadata",
params={"location": f"{lat:.8f},{lng:.8f}", "radius": radius_m, "source": "outdoor", "key": key},
timeout=5.0,
)
response.raise_for_status()
metadata = response.json()
except (httpx.HTTPError, ValueError):
return {}
return metadata if isinstance(metadata, dict) and metadata.get("status") == "OK" else {}


async def _latest_streetview_panorama(
client: httpx.AsyncClient, lat: float, lng: float, heading: float, fov: float, max_offset_m: float, key: str
) -> _StreetViewPanorama | None:
# Street View Static returns one nearest panorama per point. Probe two
# nearby rings to find other dated coverage without downloading any images.
locations = [(lng, lat)]
locations.extend(
_STREET_VIEW_GEOD.fwd(lng, lat, bearing, distance)[:2]
for distance in _STREET_VIEW_PROBE_DISTANCES_M
if distance <= max_offset_m
for bearing in range(0, 360, 45)
)
radius_m = math.ceil(max_offset_m)
responses = await asyncio.gather(
*(_streetview_metadata(client, sample_lat, sample_lng, radius_m, key) for sample_lng, sample_lat in locations)
)

# A panorama can be cropped to any heading, but sideways displacement
# changes what it sees. Narrow views allow less displacement.
max_cross_view_offset = min(_STREET_VIEW_MAX_CROSS_VIEW_OFFSET_M, max_offset_m * math.tan(math.radians(fov / 2)))
candidates: dict[str, _StreetViewPanorama] = {}
for metadata in responses:
pano_id = metadata.get("pano_id")
location = metadata.get("location")
if not isinstance(pano_id, str) or not pano_id or pano_id in candidates or not isinstance(location, dict):
continue
try:
bearing, _, offset = _STREET_VIEW_GEOD.inv(lng, lat, float(location["lng"]), float(location["lat"]))
except (KeyError, TypeError, ValueError):
continue
cross_view_offset = offset * abs(math.sin(math.radians(bearing - heading)))
if not math.isfinite(offset) or offset > max_offset_m or cross_view_offset > max_cross_view_offset:
continue
date = metadata.get("date")
if not isinstance(date, str) or not re.fullmatch(r"[0-9]{4}(?:-(?:0[1-9]|1[0-2]))?", date):
date = None
candidates[pano_id] = _StreetViewPanorama(pano_id, date, offset)

# ISO capture dates sort chronologically. An unknown month ranks before
# known months of the same year and an undated panorama ranks last.
return max(candidates.values(), key=lambda pano: (pano.date or "", -pano.offset_m), default=None)


async def fetch_streetview_plate(
lat: float,
lng: float,
Expand All @@ -41,15 +112,9 @@ async def fetch_streetview_plate(
width: int,
height: int,
*,
max_offset_m: float | None = None,
max_offset_m: float = 20,
) -> dict | None:
"""Fetch ONE real Street View photo aligned to a given camera, for use as the
composite background plate in SV-snap photomontage mode.

Snaps to the nearest pano and returns its true position + the offset from the
requested point (the accuracy cost). Returns None when there's no coverage so
the caller can fall back to the 3D-tiles capture. Never raises.
"""
"""Download one Street View photo from the newest nearby camera match."""
key = getattr(get_settings(), "google_maps_api_key", "") or ""
if not key:
return None
Expand All @@ -59,34 +124,16 @@ async def fetch_streetview_plate(
fov = max(10.0, min(120.0, float(fov)))
try:
async with httpx.AsyncClient(timeout=25.0) as client:
meta = (
await client.get(
"https://maps.googleapis.com/maps/api/streetview/metadata",
params={"location": f"{lat},{lng}", "key": key},
)
).json()
if meta.get("status") != "OK":
logger.info("sv-plate: no coverage at %.5f,%.5f", lat, lng)
return None
pano = meta["pano_id"]
ploc = meta.get("location", {})
offset_m = None
if ploc.get("lat") is not None and ploc.get("lng") is not None:
dlat = math.radians(ploc["lat"] - lat)
dlng = math.radians(ploc["lng"] - lng)
a = (
math.sin(dlat / 2) ** 2
+ math.cos(math.radians(lat)) * math.cos(math.radians(ploc["lat"])) * math.sin(dlng / 2) ** 2
)
offset_m = 2 * 6_371_000 * math.asin(math.sqrt(min(1.0, a)))
if max_offset_m is not None and (offset_m is None or offset_m > max_offset_m):
panorama = await _latest_streetview_panorama(client, lat, lng, heading, fov, max_offset_m, key)
if panorama is None:
logger.info("sv-plate: no camera-matched coverage at %.5f,%.5f", lat, lng)
return None
r = await client.get(
"https://maps.googleapis.com/maps/api/streetview",
_STREET_VIEW_URL,
params={
"pano": pano,
"pano": panorama.pano_id,
"size": f"{w}x{h}",
"heading": round(heading),
"heading": heading % 360,
"fov": round(fov),
"pitch": 0,
"return_error_code": "true",
Expand All @@ -104,8 +151,8 @@ async def fetch_streetview_plate(
return {
"photo_base64": base64.b64encode(r.content).decode(),
"mime_type": mime_type,
"pano_offset_m": round(offset_m, 1) if offset_m is not None else None,
"pano_date": meta.get("date"),
"pano_offset_m": round(panorama.offset_m, 1),
"pano_date": panorama.date,
"width": w,
"height": h,
}
Expand Down
Loading